Mismatch Error?

crburke92

New Member
Joined
Feb 5, 2019
Messages
45
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!
 

Some videos you may like

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
8,275
Office Version
  1. 365
Platform
  1. Windows
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.
 

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
8,275
Office Version
  1. 365
Platform
  1. Windows
Range("B2:B29") should be .Range("B2:B29"). See the dot/ period in red.
 

crburke92

New Member
Joined
Feb 5, 2019
Messages
45
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
 

crburke92

New Member
Joined
Feb 5, 2019
Messages
45
Nevermind....I nested the second If within the First so it was jumping over it. I'm dumb :)
 

Watch MrExcel Video

Forum statistics

Threads
1,122,892
Messages
5,598,702
Members
414,254
Latest member
MarieCo

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
Top