Simple VBA Code with Dates

Submersed

Board Regular
Joined
Jul 6, 2007
Messages
167
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Got a major headache at the minute and can't for the life of me think straight!!

I am trying to say:

If Column A (Date) is within 2 months of todays date then

Any help would be appreciated...im off to find tablets!

Thanks
 
Last edited:

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
What problem are you having?

If we assume 60 days for 2 months, then this is OK

=IF(AND(NOW()-60<=a3,NOW()+60>=a3),"True","False")

or is it working out the 2 months either side of now ?
 
Upvote 0
What problem are you having?

If we assume 60 days for 2 months, then this is OK

=IF(AND(NOW()-60<=a3,NOW()+60>=a3),"True","False")

or is it working out the 2 months either side of now ?


Sorry, should of explained, I am trying to do it within VBA....
 
Upvote 0
Here's a formula:

=IF(AND(A3>=NOW()-60,A3<=NOW()+60),"Yes","No")

If you want to do VBA code:

Code:
Dim i As Integer
    For i = 1 To 10
        If Range("A" & i).Value >= Now() - 60 And Range("A" & i).Value <= Now() + 60 Then
            MsgBox Range("A" & i).Address & " is within 60 days of today."
        End If
    Next i
 
Upvote 0
Hi, Try :-
Code:
[COLOR="Navy"]Sub[/COLOR] MG04Aug56
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Dn [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
  [COLOR="Navy"]If[/COLOR] Dn > DateAdd("m", -2, Date) And Dn < DateAdd("m", 2, Date) [COLOR="Navy"]Then[/COLOR]
    Dn.Interior.ColorIndex = 6
  [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,237
Members
448,555
Latest member
RobertJones1986

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