I am unsure why this code only functions properly with the inclusion of a seemingly inconsequential MsgBox line. If I try to run this code with the "MsgBox (offdays)" line commented out, I receive the error msg of
"Run-time error '13': Type mismatch".
The strings being passed into the code are in a string form, (Example: Jun 0902:00 AM), and reside in the first column of the data sheets. I am trying to determine if the 1st column of these data sheets are in sequential order from top to bottom, (Example: Cell(1,1) = Dec 31, Cell(2,1) = Jun 20, Cell(3,1) = Jan 1). For some reason the code works fine only when the MsgBox(offdays) line is included. Obviously it is unviable to close thousands of msgbox's manually and I have yet to find a different workaround and I am also unsure why this would even change how it works. Any advice is appreciated.
Current version of code:
Public Sub validSheetVerifier()
Dim sheetcounter As Integer
If VALIDSHEETORDER = True Then
MsgBox ("Valid Sheet Order")
For sheetcounter = 4 To Worksheets.Count
Worksheets(sheetcounter).Select
Call SEQUENTIALDATECHECK(ActiveSheet)
MsgBox ("Valid Sheet")
Next
Else
MsgBox ("error invalidsheetorder")
End If
End Sub
Function VALIDSHEETORDER() As Boolean
If Worksheets(1).Name = "DATA" And Worksheets(2).Name = "TEST" And Worksheets(3).Name = "RESULTS" Then
VALIDSHEETORDER = True
Else
VALIDSHEETORDER = False
End If
End Function
Function SEQUENTIALDATECHECK(currentsheet As Worksheet) As Boolean
Dim rowcounter As Integer
Dim nextrow As Integer
Dim offdays As Integer
For rowcounter = 2 To TOLASTROW(currentsheet)
nextrow = rowcounter + 1
offdays = WorksheetFunction.Days(TOMATCHDAY(rowcounter), TOMATCHDAY(rowcounter + 1))
' MsgBox (offdays)
If offdays < 0 Then
MsgBox ("error " & ActiveSheet.Name & " row: " & rowcounter)
Else
End If
Next
End Function
Function TOLASTROW(currentsheet As Worksheet) As Integer
TOLASTROW = currentsheet.Cells(Rows.Count, 1).End(xlUp).Row
End Function
Function TOMATCHSTRING(currentrow As Integer) As String
TOMATCHSTRING = Cells(currentrow, 1)
End Function
Function TOMATCHDAY(currentrow As Integer) As Date
Dim matchday As String
matchday = Left(TOMATCHSTRING(currentrow), 6)
TOMATCHDAY = CDate(matchday)
End Function
Edit: Partial Sample Data Set
Jun 0902:00 AM
Jun 0806:35 AM
Jun 0806:00 AM
Jun 0803:40 AM
Jun 0801:55 AM
Jun 0706:35 AM
Jun 0706:00 AM
Jun 0703:40 AM
Jun 0701:55 AM
Jun 0407:00 AM
Jun 0405:40 AM
Jun 0405:05 AM
Jun 0404:00 AM
Jun 0402:30 AM
Jun 0402:00 AM
Jun 0306:30 AM
Jun 0306:05 AM
Jun 0305:00 AM
Jun 0303:35 AM
Jun 0303:05 AM
Jun 0302:05 AM
Jun 0102:35 PM
Jun 0101:20 PM
Jun 0111:45 AM
Jun 0111:15 AM
Jun 0109:55 AM
Jun 0109:20 AM
Jun 0107:45 AM
May 3006:55 AM
May 3006:00 AM
May 3005:10 AM
May 3003:40 AM