I've been stumped on this problem for a few days now: a
"Run-time error '13': Type mismatch"
when I'm trying to compare dates. I have looked my problem up to see if anyone else has run into the same issues, and found this. I have tried implementing the solution to that question with no luck.
I'm working with a userform called calendar that holds dates in labels overlayed on correlating textboxes that the user can edit, and place notes into as shown here.
I save the notes to column B in the worksheet and the date of the note in column A.
Right now, I'm trying to pull the notes from the worksheet and place them into the textboxes by comparing dates and this is where I'm having problems. When I compare the dates I pull the label caption and save it as date curDate
, and pull the worksheet date and save it as wsDate
.I initially had a message box in the if statement (now commented out) to see if the code was running ... and it is, sort of. It will run until the first match and then give the run-time error.
Dim ws As Worksheet
Dim lrow As Integer 'last row
Dim row As Integer 'used for looping through rows
Dim wsDate As Date 'worksheet date
Dim curDate As Date 'label date
Dim i As Integer
Set ws = Sheets("Hidden Information")
lrow = ws.Range("A" & ws.Rows.Count).End(xlUp).row
With ws 'Hidden Information worksheet
For i = 1 To 38 'number of labels
For row = 2 To lrow 'runs til the last row on the worksheet
wsDate = DateSerial(Year(.Cells(row, 1).Value), _
Month(.Cells(row, 1).Value), Day(.Cells(row, 1).Value))
curDate = DateSerial(Year(Controls("Label" & i).Caption), _
Month(Controls("Label" & i).Caption), Day(Controls("Label" & i).Caption))
If wsDate = curDate Then Controls("TextBox" & i).Text = vbNewLine & _
vbNewLine & .Cells(row, 2).Value 'MsgBox "it's a match!"
Next row
Next i
End With
I've reworked the code a few times, and tried saving the dates in different ways such as,
temp = CDate(.Cells(row, 1).Value)
wsDate = Format(temp, "mm/dd/yy")
temp = CDate(Controls("Label" & i).Caption)
curDate = Format(temp, "mm/dd/yy")
But it has the same problem: would run 'til the first match and then give the runtime error.
I have a feeling the solution is something obvious that I can't see because I've been staring at this for a few days now...
If anything needs to be clarified please let me know.