data on my Excel sheet我想在A栏中找到一个日期。这是日期形式:“yyyy / mm / dd hh:mm:ss”。它总是找不到任何东西,但是我要搜索的日期在A列中。 这是我的代码片段:
Dim LastDay As Date
Dim strdate As String
Dim rCell As Range
strdate = Format(LastDay, "yyyy/mm/dd hh:mm:ss")
Set rCell = Cells.Find(What:=CDate(strdate), After:=Range("A1"), LookIn:=xlValues _
, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
If rCell Is Nothing Then
MsgBox ("nothing")
Else
编辑:每条评论的新代码。
Sub Copy()
Dim LastDayRow As Long
Dim FirstDayRow As Long
Dim LastDay As Date
Dim FirstDay As Date
Dim rcell As Range
LastDayRow = Range("E" & Rows.Count).End(xlUp).Row
Range("E" & LastDayRow).Copy Range("G1")
FirstDayRow = Range("A" & Rows.Count).End(xlUp).Row
Range("A" & FirstDayRow).Copy Range("G2")
LastDay = Cells(LastDayRow, "E").Value
FirstDay = Cells(FirstDayRow, "A").Value
Set rcell = Cells.Find(What:=LastDay, After:=Range("A1"), LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
If rcell Is Nothing Then
MsgBox ("nothing")
End If
End Sub
答案 0 :(得分:0)
日期类型以Excel格式存储在Excel和VBA中。通过将LookIn参数更改为xlFormulas
来搜索double值而不是date。您也可以省略MatchCase参数(您使用的是默认值)。
Set rCell = Cells.Find(What:=LastDay, After:=Range("A1"), LookIn:=xlFormulas _
, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext)
请注意,你在这里做了一些毫无意义的工作......
strdate = Format(LastDay, "yyyy/mm/dd hh:mm:ss")
...因为您只是在执行查找时将 返回 投射到日期:
What:=CDate(strdate)
答案 1 :(得分:0)
问题是约会的形式。此日期格式中的“/”:yyyy / mm / dd hh:mm:ss混淆了vba运行。 源文件中的这些代码行解决了这个问题:
Sheet1.Range("A:A").Replace What:="/", Replacement:="."
Sheet1.Range("A:A").NumberFormat = "yyyy/mm/dd hh:mm:ss"
Sheet3.Range("E:E").Replace What:="/", Replacement:="."
Sheet3.Range("E:E").NumberFormat = "yyyy/mm/dd hh:mm:ss"