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

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).

WaterGypsy

Well-known Member
Joined
Jan 15, 2010
Messages
697
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 ?
 

Submersed

Board Regular
Joined
Jul 6, 2007
Messages
167
Office Version
  1. 365
Platform
  1. Windows
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....
 

doofusboy

Well-known Member
Joined
Oct 14, 2003
Messages
1,325
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
 

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
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
 

Submersed

Board Regular
Joined
Jul 6, 2007
Messages
167
Office Version
  1. 365
Platform
  1. Windows
Thats perfect guys! Nice one!

Thanks a lot!
 

Watch MrExcel Video

Forum statistics

Threads
1,133,032
Messages
5,656,508
Members
418,309
Latest member
edy tang

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