我是VBA的新手,我不确定如何完成以下任务。
我想问用户一个日期范围,然后选择该日期范围,并在“资料表”的BB栏中搜索该日期范围内的任何日期。如果日期在该范围内,我要整行复制,然后将其粘贴到另一个名为“目标表”的表中
感谢您的帮助!我尝试了许多不同的方法来执行此操作,但我没有做任何工作。这是我目前拥有的
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
答案 0 :(得分:0)
我认为以下代码可能是您想要的。
但是,变量“ j”的目的是什么?另外,BB列中的值是否未格式化为日期?我不确定执行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