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:
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?
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:
- Click a button to open File window
- Select data file (book1 in this case)
- Popups asking for start and end times
- 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 ??)
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?