Reputation: 1
If I run this code I get an error but can't find the reason, is someone able to help me out please
If Format(DateAdd("d", (i - Weekday((CB_Mth.Value) & "/1/" & (CB_Yr.Value))), _
((CB_Mth.Value) & "1/1" & (CB_Yr.Value))), "mmm") = ((CB_Mth.Value)) Then
If Controls("D" & (i)).BackColor <> &H99FF33 Then Controls("D" & (i)).BackColor = &H99FF33
Controls("D" & (i)).Font.Bold = True
If Format(DateAdd("d", (i - Weekday((CB_Mth.Value) & "/1/" & (CB_Yr.Value))), _
((CB_Mth.Value) & "1/1" & (CB_Yr.Value))), "d/m/yyyy") = Format(ThisDay, "d/m/yy") Then Controls("D" & (i)).SetFocus
Else
If Controls("D" & (i)).BackColor <> &H99FF33 Then Controls("D" & (i)).BackColor = &H99FF33
Controls("D" & (i)).Font.Bold = False
End If
Next
End Sub
Upvotes: 0
Views: 71
Reputation: 929
The error you are receiving is most likely due to your DateAdd functions expecting a date, However you are providing it with CB_Mth.Value & "1/1" & CB_Yr.Value
which would come out as: <Month>1/1<Year>
. I assume you ment <Month>/1/<Year>
Personal opinion: use ISO-8601 date format
Below is my attempt at cleaning up the code to read it. I may or may not have grouped your if statements correctly (not sure exactly what you were trying to do).
If Format(DateAdd("d", i - Weekday(CB_Mth.Value & "/1/" & CB_Yr.Value), CB_Mth.Value & "/1/" & CB_Yr.Value), "mmm") = CB_Mth.Value Then
If Controls("D" & i).BackColor <> &H99FF33 Then
Controls("D" & i).BackColor = &H99FF33
Controls("D" & i).Font.Bold = True
End If
End If
If Format(DateAdd("d", i - Weekday(CB_Mth.Value & "/1/" & CB_Yr.Value), CB_Mth.Value & "/1/" & CB_Yr.Value), "d/m/yyyy") = Format(ThisDay, "d/m/yy") Then
Controls("D" & i).SetFocus
Else
If Controls("D" & i).BackColor <> &H99FF33 Then
Controls("D" & i).BackColor = &H99FF33
Controls("D" & i).Font.Bold = False
End If
End If
Next
End Sub
Upvotes: 1