I open my workbook and run my macro and all code (that I can tell) executes fine. Without changing any data I try and run the macro again and I get an error. The error specifically happens with .Find and is returning Nothing when in fact there is a date to be found in the specified range.
I have gone through the debug tool and by all accounts I do not know why .Find is returning Nothing on the second Macro run and not the first.
The line in question is:
Set DateFind = .Find(what:=TripFind.Offset(0, 2).Value, LookAt:=xlWhole, _
MatchCase:=False, SearchFormat:=False)
The first time running the Macro DateFind Returns the correct value. The second time the Macro is run DateFind Returns Nothing but on all accounts should be returning the same value as on the first run of the Macro.
Here is the full code section that is in question: **The first part of the code runs fine. The second part starting at 'Adds day numbers... etc. is the important part.*
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"