VBA to copy dynamic range from different workbook

Akira181

Board Regular
Joined
Mar 23, 2010
Messages
67
Office Version
  1. 365
Platform
  1. Windows
I'm still a novice to VBA so I cannot figure this out for the life of me, some help would be much appreciated!

I have a workbook (Book1 in the example below) with a bunch of data. Every row has a timestamp in column A. There are 7 columns but this could change

In Book2, I would like my VBA to do the following:
  1. Click a button to open File window
  2. Select data file (book1 in this case)
  3. Popups asking for start and end times
  4. VBA finds the start and end times and copies those rows across into sheet 1 (ideally starting from A2, but one thing at a time ?‍?)
It's 4 that I cannot get to work. Below is my code:

VBA Code:
Private Sub CommandButton1_Click()
Dim FP As Variant
Dim wrk As Workbook
Dim wsCopy As Worksheet

FP = Application.GetOpenFilename(FileFilter:="Excel Files (*.XLSX), *.XLSX", Title:="Select File To Be Opened")
If FP = False Then Exit Sub

Set wrk = Workbooks.Open(Filename:=FP)
Set wsCopy = wrk.Worksheets("sheet1")


BeginTime = InputBox("Please enter your start time.")
EndTime = InputBox("Please enter your end time.")


Set BeginRng = wsCopy.Cells.Find(What:=BeginTime, _
                                        LookIn:=xlValues, _
                                        Lookat:=xlPart, _
                                        SearchOrder:=xlByRows, _
                                        SearchDirection:=xlNext, _
                                        MatchCase:=False)
   
Set EndRng = wsCopy.Cells.Find(What:=EndTime, _
                                        LookIn:=xlValues, _
                                        Lookat:=xlPart, _
                                        SearchOrder:=xlByRows, _
                                        SearchDirection:=xlNext, _
                                        MatchCase:=False)


If BeginRng Is Nothing Then
    MsgBox "No match found." & vbNewLine & "Please enter a valid starting time."
ElseIf EndRng Is Nothing Then
    MsgBox "No match found." & vbNewLine & "Please enter a valid ending time."
Else

' Get the total Columns in the source file.
Dim iColumnsCount As Integer
    iColumnsCount = wsCopy.UsedRange.Columns.Count
   
Dim iRows, iCols, iStartRow As Integer
    iStartRow = 0

' Now, read the source and copy data to the master file.
For iRows = BeginRng To EndRng
    For iCols = 1 To iColumnsCount
        Cells(iRows + iStartRow, iCols) = wsCopy.Cells(iRows, iCols)
    Next iCols
Next iRows
   
    iStartRow = iRows + 1
    iRows = 0
End If

End Sub

I think my issue is that BeginRng and EndRng are not returning cell values but rather what I'm entering in the popups. Can someone tell me how to fix this?
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Any help? Tried doing it a different way but made it worse.

My timestamp format is in hh:m:ss.000 if that's relevant
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,246
Members
452,623
Latest member
cliftonhandyman

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top