VBA to check if today's date in range if not skip

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
878
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I am looking for a line of code to stop my exiting code from running if it cannot find today's today in YYYYMMDD format in column B.

1734477177560.png
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
What is the format of the cells in column B?

Are they valid dates with a "yyyymmdd" format?
Or are they just a General or Numeric format (meaning the value is the cell is really the number 20241218 and not a date)?
 
Upvote 0
OK, so those entries are not dates at all, but rather numbers.

You should be able to use this block of code to do what you want:
VBA Code:
'   Convert todays date to a number that looks like yyyymmdd
    Dim d As Long
    d = Format(Date, "yyyymmdd") + 0
    
'   See if the current date is found anywhere in column B
    If Application.WorksheetFunction.CountIf(Range("B:B"), d) > 0 Then
        'whatever you want to happen here if the date is found in column B
    End If
 
Upvote 0
I may be interpreting/translating this incorrectly. But I am looking to end my VBA if there are no dates that equal today. The below is jumping to the ELSE therefore as if its finding dates for today?

VBA Code:
    Dim d As Long
    
Application.ScreenUpdating = False
Application.EnableEvents = False

'set shortcut for with sheets

'set shortcut without "with"

'Use either workbook extension to avoid file extension option issue
    
d = Format(Date, "yyyymmdd") + 0
    
'check if any ex dates to evaluate if not end sub
If Application.WorksheetFunction.CountIf(Range("B:B"), d) = 0 Then

With WsRUN
    .Activate
End With
    
MsgBox "No activity for Today"
    
Else
    'if there are dates run other code
 
Upvote 0

Forum statistics

Threads
1,224,698
Messages
6,180,423
Members
452,981
Latest member
MarkS1234

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