因此,我有一个包含多个工作表的工作簿,每个工作表中的每一行都是针对不同的产品,并且具有产品到达的日期以及其他一些信息 .
我有一张名为“GRN-Date Search”的工作表,我允许用户输入特定信息并让VBA搜索工作表并复制和粘贴信息 .
在谈到搜索用户定义的日期范围时,我遇到了障碍 .
以下是我为一个日期提供的想法 . 我是VBA的新手,所以我不确定是否可以将.find函数用于日期范围?
您可以提供的任何帮助将不胜感激 .
Sub DateSearch_Click()
If Range("B3") = "" Then
MsgBox "You must enter a date to search"
Range("B3").Select
Exit Sub
Else
'Clear "GRN-Date Search" Sheet Row through End
Sheets("GRN-Date Search").Range("A7:A" & Rows.Count).EntireRow.Clear
'Set myDate variable to value in B3
myDate = Sheets("GRN-Date Search").Range("B3")
'Set initial Paste Row
nxtRw = 7
'Loop through Sheets 2 - 29
For shtNum = 2 To 29
'Search Column b for date(s)
With Sheets(shtNum).Columns(1)
Set d = .Find(myDate)
If Not d Is Nothing Then
firstAddress = d.Address
Do
'Copy each Row where date is found to next empty Row on Summary sheet
d.EntireRow.Copy Sheets("GRN-Date Search").Range("A" & nxtRw)
nxtRw = nxtRw + 1
Set d = .FindNext(d)
Loop While Not d Is Nothing And d.Address <> firstAddress
End If
End With
Next
End If
End Sub
1 回答
要使用日期范围,您需要放弃使用
.Find
. 最好的方法是使用自动过滤 . 以下代码使用此功能,并假设您的用户在单元格B3
和C3
中输入一系列日期 . 还记得autofilter
认为您在过滤范围内有一个 Headers 行 .