【发布时间】:2016-08-09 13:59:24
【问题描述】:
data on my Excel sheet我想在 A 列中找到一个日期。这是日期格式:“yyyy/mm/dd hh:mm:ss”。它总是一无所获,但我要搜索的日期在 A 列中。 这是我的代码的 sn-p:
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
编辑:每个 cmets 的新代码。
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
【问题讨论】:
-
从您提供的代码来看,您似乎没有为
LastDay变量设置任何值。 -
你写的代码是基于 xlValue 找到的。单元格中存储的日期实际上只是数字。因此,您将无法使用 LookIn:=xlValue 找到它
-
也可能是 format( 将日期转换为字符串格式;而您正在寻找一个日期
-
我建议您尝试使用 LookIn:=XlFormulas, LookAt:=xlPart