0

Im kinda new with Excel VBA, and I'm stuck with something, I've tried some stuff but I dont know enough to get it right.

Here is the thing, I have a form in a workbook1 in which I select a start date and end date from a calendar, once selected I press a button and I have to copy from a closed file let's call workbook2 all the elements from that start date to the end date.

So if I select from 19-08-2013 to 25-08-2013, I want Element 2 to Element 11 to be copied to workbook1

Workbook2(thousands of elements dates, etc):

 ╔═══╦════════════╦═════════════╦═════════════╦═════════════╦═════════════╗
 ║   ║    A       ║      B      ║      c      ║      D      ║      E      ║
 ╠═══╬════════════╬═════════════╬═════════════╬═════════════╬═════════════╣
 ║ 1 ║ Type       ║ Element 1   ║             ║ 16-08-2013  ║ 18-08-2013  ║
 ║ 1 ║ Type       ║ Element 2   ║             ║ 19-08-2013  ║ 22-08-2013  ║
 ║ 2 ║ Header     ║ Element 3   ║             ║ 19-08-2013  ║ 22-08-2013  ║
 ║ 3 ║ Auto Align ║ Element 4   ║             ║ 19-08-2013  ║ 22-08-2013  ║
 ║ 4 ║ Auto Align ║ Element 5   ║             ║ 19-08-2013  ║ 22-08-2013  ║
 ║ 5 ║ Auto Align ║ Element 6   ║             ║ 19-08-2013  ║ 22-08-2013  ║
 ║ 6 ║ Auto Align ║ Element 7   ║             ║ 23-08-2013  ║ 25-08-2013  ║
 ║ 7 ║ Auto Align ║ Element 8   ║             ║ 23-08-2013  ║ 25-08-2013  ║
 ║ 8 ║ Auto Align ║ Element 9   ║             ║ 23-08-2013  ║ 25-08-2013  ║
 ║ 9 ║ Auto Align ║ Element 10  ║             ║ 23-08-2013  ║ 25-08-2013  ║
 ║10 ║ Auto Align ║ Element 11  ║             ║ 23-08-2013  ║ 25-08-2013  ║
 ║11 ║ Auto Align ║ Element 12  ║             ║ 26-08-2013  ║ 01-09-2013  ║
 ║12 ║ Auto Align ║ Element 13  ║             ║ 26-08-2013  ║ 01-09-2013  ║
 ║13 ║ Auto Align ║ Element 14  ║             ║ 26-08-2013  ║ 01-09-2013  ║
 ║14 ║ Auto Align ║ Element 15  ║             ║ 26-08-2013  ║ 01-09-2013  ║
 ║15 ║ Auto Align ║ Element 16  ║             ║ 26-08-2013  ║ 01-09-2013  ║
 ║.. ║    ...     ║     ...     ║     ...     ║     ...     ║     ...     ║
 ║ n ║     n      ║ Element n   ║             ║  start date ║ end date    ║
 ╚═══╩════════════╩═════════════╩═════════════╩═════════════╩═════════════╝

workbook1:

╔═══╦════════════╗
║   ║    A       ║
╠═══╬════════════╣
║ 1 ║ Element 2  ║
║ 2 ║ Element 3  ║
║ 3 ║ Element 4  ║
║ 4 ║ Element 5  ║
║ 5 ║ Element 6  ║
║ 6 ║ Element 7  ║
║ 7 ║ Element 8  ║
║ 8 ║ Element 9  ║
║ 9 ║ Element 10 ║ 
║10 ║ Element 11 ║
╚═══╩════════════╝

This is what I have for my update(actualizar) button so far:

Private Sub actualizar_Click()

If calendario.SelStart + 6 = calendario.SelEnd Then //calendario is the calendar
    Sheets("variables").Range("B1").Value = calendario.SelStart //i just copy the
    Sheets("variables").Range("B2").Value = calendario.SelEnd   //selected date to wb1

    '///// code to get data

    Dim wb As Workbook
    Application.ScreenUpdating = False ' turn off the screen updating
    Set wb = Workbooks.Open("C:\Users\G\Desktop\AnalyticsBuilder\Panel a completarCOPIA.xlsx", True, True)
    ' open the source workbook, read only


    Dim c As Range
    Dim x As Range
    Set x = Range("C5")

    For Each c In wb.Worksheets("2012").Range("K:K")
        If c.Value >= calendario.SelStart And c.Value <= calendario.SelEnd Then

          ThisWorkbook.Worksheets("variables").x.Value = wb.Worksheets("2012").c.Value

        End If
    Next c


    wb.Close False ' close the source workbook without saving any changes
    Set wb = Nothing ' free memory

    Application.ScreenUpdating = True ' turn on the screen updating
    Unload Me

ElseIf calendario.SelStart + 6 <> calendario.SelEnd Then
    MsgBox ("Seleccionar semana completa"), , "Error"
End If

End Sub

I've succeed in trying to copy a cell from the closed wb2 but this code to get the element is not working.

Also copying from the closed wb makes excel freeze for a couple second before getting the data, is there a way to fix that?

Hope you can help with this, Thank you in advance.

Community
  • 1
  • 1
gepex
  • 197
  • 1
  • 2
  • 14

2 Answers2

0

Look here .But below is something i use sometimes.

Function xFind2B(xS As Worksheet)
    On Error Resume Next

    Dim c As Range
    Dim xRng As Range
    Set xRng = xS.Range("D1:D20")

    For Each c In xRng
        If c.Value => textbox.value And <= textbox2.value Then
            'Do Your copy
            Exit For
        End If
    Next c

    xFind2BlanksA = c.Offset(-1).Row

    On Error GoTo 0
End Function
Community
  • 1
  • 1
D_M
  • 134
  • 1
  • 10
0

Perhaps...

Private Sub actualizar_Click()

    Dim wsDest As Worksheet
    Dim DateCell As Range
    Dim arrResults(1 To 65000) As Variant
    Dim ResultIndex As Long

    Set wsDest = ThisWorkbook.Sheets("variables")

    If calendario.SelStart + 6 = calendario.SelEnd Then '//calendario is the calendar

        wsDest.Range("B1:B2").Value = Application.Transpose(Array(calendario.SelStart, calendario.SelEnd))

        Application.ScreenUpdating = False ' turn off the screen updating
        With Workbooks.Open("C:\Users\G\Desktop\AnalyticsBuilder\Panel a completarCOPIA.xlsx", True, True)

            For Each DateCell In Intersect(.Sheets("2012").UsedRange, .Sheets("2012").Columns("K"))
                If IsDate(DateCell.Value) Then
                    If DateCell.Value >= calendario.SelStart And DateCell.Value <= calendario.SelEnd Then
                        ResultIndex = ResultIndex + 1
                        arrResults(ResultIndex) = DateCell.Text
                    End If
                End If
            Next DateCell
            .Close False
        End With

        If ResultIndex > 0 Then wsDest.Cells(Rows.Count, "C").End(xlUp).Offset(1).Resize(ResultIndex).Value = Application.Transpose(arrResults)

        Application.ScreenUpdating = True ' turn on the screen updating
        Unload Me

    ElseIf calendario.SelStart + 6 <> calendario.SelEnd Then
        MsgBox ("Seleccionar semana completa"), , "Error"
    End If

End Sub
tigeravatar
  • 26,199
  • 5
  • 30
  • 38