I am new to VBA and I am not sure how I can accomplish the following task.
I want to ask the user for a date range and then take that date range and search through column BB of “Source Sheet” for any date in that range. If a date is in that range I want to take the entire row and copy it and then paste it into a different sheet called “Dest Sheet”
Any help is appreciated! I have tried many different ways of doing this and nothing I have done is working. Here is what I currently have
Dim N As Integer
Dim i As Integer
Dim StartDate As Date
Dim EndDate As Date
N = Cells(Rows.Count, "E").End(xlUp).Row 'determines the last row with data in it
'uses column E because E should never be Null if not after last active row
Dim j As Integer 'declares j for indexing of the rows on the target spreadsheet
j = 2
Dim fRow As Long 'is the role of j in an attempt to copy and paste
fRow = Sheets("Dest Sheet").UsedRange.Rows.Count 'defines the variable
For i = 2 To N 'indexes 2 to N to begin with row after the title row to the last active ro
Cells(i, "BB").Select
If Cells(i, "BB").Value <> "" Then
Columns("BB").Select
Selection.NumberFormat = "mm/dd/yyyy"
Range("BB2").End(xlDown).Select
StartDate = Application.InputBox("Enter the start date")
EndDate = Application.InputBox("Enter the end date")
'in row i execute the if statement
If ("BB" >= StartDate & "BB" <= EndDate) Then
Sheets("Source Sheet").Cells(i, 1).EntireRow.Copy Destination:=Sheets("Dest Sheet").Cells(fRow, 1).Offset(1, 0)
fRow = fRow + 1
End If
End If 'declares end of the if statements
Next i
End Sub
I think the below code might be what you are looking for.
However, what is the purpose of your variable "j"? Also, are the values in column BB not formatted as dates? I wasn't sure the purpose of the line that executes Selection.NumberFormat = "mm/dd/yyyy"
Dim wb As Workbook
Dim wsSrc As Worksheet
Dim wsDest As Worksheet
Dim n As Long
Dim i As Long
Dim fRow As Long
Dim startDate As Date
Dim endDate As Date
Set wb = ActiveWorkbook
Set wsSrc = wb.Sheets("Source Sheet")
Set wsDest = wb.Sheets("Dest Sheet")
n = wsSrc.Range("E:E").Find(what:="*", searchdirection:=xlPrevious).Row
startDate = Application.InputBox("Enter the start date")
endDate = Application.InputBox("Enter the end date")
For i = 2 To n
If wsSrc.Range("BB" & i).Value >= startDate And wsSrc.Range("BB" & i).Value <= endDate Then
fRow = wsDest.Range("A:A").Find(what:="").Row
wsSrc.Range("BB" & i).EntireRow.Copy wsDest.Cells(fRow, 1)
End If
Next
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.