Mismatch Error?

crburke92

Board Regular
Joined
Feb 5, 2019
Messages
71
Having a mismatch error trying to compare a range of cells (Which is my date column) to todays date. I have Cell A1 =TODAY(), and range B2:B29 are expiry dates. I initially had the message box within the "For each cell" statement, which did work and format the cells properly, but then gave the message box for every expired date it found. Tried bringing the message box outside of the For Statement so it only came up once and BAM! Mismatch error.

Code:
Private Sub Workbook_Open()
Dim TDate As Date
Dim ExDate As Date
TDate = Range("A1")
ExDate = Range("B2:B29")
If TDate < ExDate - 10 Then
    MsgBox "Something is about to expire!"
End If
For Each Cell In Range("B2:B20")
    If Cell.Value < TDate - 10 And Cell.Value <> "" Then
            Cell.Interior.ColorIndex = 19
            Cell.Font.Bold = True
                If Cell.Value > TDate - 10 And Cell.Value <> "" Then
                    Cell.Interior.ColorIndex = 2
                    Cell.Font.Bold = False
                End If
    End If
Next
End Sub

Thanks in advance!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Try like this:

Code:
Dim TDate As Long, ExDate As Long

With Sheets("Sheet1")
    TDate = .Range("A1")
    ExDate = .Application.Min(Range("B2:B29"))
    If TDate > ExDate - 10 Then MsgBox "Something is about to expire!"
End With

You need to use the sheet name really.
 
Upvote 0
Range("B2:B29") should be .Range("B2:B29"). See the dot/ period in red.
 
Upvote 0
That did the trick for the message box, but now I'm having an issue with my last If statememnt to revert the formatting back? Any ideas why?

Code:
Private Sub Workbook_Open()
Dim TDate As Long
Dim ExDate As Long
With Sheets("Sheet1")
    TDate = .Range("A1")
    ExDate = Application.Min(Range("I3:I29"))
    If ExDate < TDate - 10 Then MsgBox "Something is about to expire!"
End With
For Each Cell In Range("I3:I29")
    If Cell.Value < TDate - 10 And Cell.Value <> "" Then
            Cell.Interior.ColorIndex = 19
            Cell.Font.Bold = True
                If Cell.Value > TDate - 10 And Cell.Value <> "" Then
                    Cell.Interior.ColorIndex = 2
                    Cell.Font.Bold = False
                End If
    End If
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top