我打开我的工作簿并运行我的宏,所有代码(我可以告诉)执行正常。在不更改任何数据的情况下,我尝试再次运行宏,然后出错。 .Find
特别发生错误,并且在实际上在指定范围内找到日期时返回Nothing
。
我已经完成了调试工具,所有帐户我不知道为什么.Find
在第二次宏运行时返回Nothing
而不是第一次。
有问题的一行是:
Set DateFind = .Find(what:=TripFind.Offset(0, 2).Value, LookAt:=xlWhole, _
MatchCase:=False, SearchFormat:=False)
第一次运行宏DateFind
返回正确的值。第二次运行宏DateFind
返回Nothing
,但所有帐户应返回与第一次运行宏时相同的值。
以下是有问题的完整代码部分: **代码的第一部分运行正常。第二部分从'添加日期数字......等是重要的部分。*
ElseIf TripCal.Range("A1") = "SESSION 2" Then
'-----Copies and pastes cabin numbers into tripcal-----
For TotalRowsOffered = 5 To 168
If TotalRowsOffered >= Level1Offered And TotalRowsOffered < Level2Offered And TripsOffered.Cells(TotalRowsOffered, Session2) <> "" Then
a = TotalRowsOffered - 6
TripCal.Cells(TotalRowsOffered + a, "B") = TripsOffered.Cells(TotalRowsOffered, Session2).Value
ElseIf TotalRowsOffered >= Level2Offered And TotalRowsOffered < Level3Offered And TripsOffered.Cells(TotalRowsOffered, Session2) <> "" Then
a = TotalRowsOffered - 8
TripCal.Cells(TotalRowsOffered + a, "B") = TripsOffered.Cells(TotalRowsOffered, Session2).Value
ElseIf TotalRowsOffered >= Level3Offered And TotalRowsOffered < Level4Offered And TripsOffered.Cells(TotalRowsOffered, Session2) <> "" Then
a = TotalRowsOffered - 10
TripCal.Cells(TotalRowsOffered + a, "B") = TripsOffered.Cells(TotalRowsOffered, Session2).Value
ElseIf TotalRowsOffered >= Level4Offered And TotalRowsOffered < Level5Offered And TripsOffered.Cells(TotalRowsOffered, Session2) <> "" Then
a = TotalRowsOffered - 12
TripCal.Cells(TotalRowsOffered + a, "B") = TripsOffered.Cells(TotalRowsOffered, Session2).Value
ElseIf TotalRowsOffered >= Level5Offered And TripsOffered.Cells(TotalRowsOffered, Session2) <> "" Then
a = TotalRowsOffered - 14
TripCal.Cells(TotalRowsOffered + a, "B") = TripsOffered.Cells(TotalRowsOffered, Session2).Value
End If
Next
'-----Adds day number of trip for each cabin-----
For TripCounter = 4 To 323
If TripCal.Cells(TripCounter, "B") = "" Then
'Skips if there is no trip name accounted for _
beside the level on the Trip Calender
Else
With TripsOffered.Range(TripsOffered.Cells(5, Session2), TripsOffered.Cells(168, Session2))
Set TripFind = .Find(what:=TripCal.Cells(TripCounter, "B"), LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not TripFind Is Nothing Then
Tripdays = TripFind.Offset(0, 4).Value - TripFind.Offset(0, 2).Value
With TripCal.Range(TripCal.Cells(1, 3), TripCal.Cells(1, LastDate))
Set DateFind = .Find(what:=TripFind.Offset(0, 2).Value, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not DateFind Is Nothing Then
For TripDayCount = 0 To Tripdays
TripCal.Cells(TripCounter, DateFind.Column + TripDayCount) = TripDayCount + 1
Next
Else
MsgBox ("The Trip Date for " & TripFind.Value & " is outside of the current session dates." & vbNewLine & vbNewLine & "Please check the trip dates in the 'Trips Being Offered' sheet for " & TripFind.Value & " in Session 2.")
End If
End With
End If
End With
End If
TripCounter = TripCounter + 1
Next
ElseIf TripCal.Range("A1") = "SESSION 3" Then
'the above code repeat depending on Range "A1"
答案 0 :(得分:2)
.Find
会关注活动单元格 - 如果您将该行更改为Set DateFind = .Find(what:=TripFind.Offset(0, 2).Value, After:=TripsOffered.Range(TripsOffered.Cells(5, Session2), LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
检查所有单元格
答案 1 :(得分:0)
Range.Find方法的参数是可选的。但在查找日期时,强烈建议始终设置Lookin参数
Set TripFind = .Find(what:=CDate(TripCal.Cells(TripCounter, "B")), LookIn:=xlFormulas, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
如果未设置Find参数,则使用现有设置。