VBA If statement

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
589
Dear All,

I have two dates, where I'd like a message box to pop, if the dates are between this criteria.

Code:
    sDate1 = #10/1/2019#
    sDate2 = #9/30/2020#

How would I create the following?

Code:
 If date is between sDate1 & sDate2 Then
                                        
             MsgBox " Dates Within Range" 
   
End If
Thank you!
pinaceous
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
7,920
Office Version
2007
Platform
Windows
Try this

Code:
Sub test()
  Dim sDate1 As Date, sDate2 As Date
  sDate1 = #10/1/2019#
  sDate2 = #9/30/2020#
  If Date >= sDate1 And Date <= sDate2 Then
    MsgBox "Dates Within Range"
  End If
End Sub
 

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
589
Hey Dante Amor,

In building upon this concept if I have nothing listed in this range I can then write:

Code:
Sub test()
  Dim sDate1 As Date, sDate2 As Date
  sDate1 = #10/1/2019#
  sDate2 = #9/30/2020#
  If Date >= sDate1 And Date <= sDate2 Then
    MsgBox "Dates Within Range"
  End If


 If Date <> Empty Then
    
        MsgBox " NO Dates Listed " 
   
  End If 

End Sub
But now how could I combine these two codes together onto one Sub?

Thank you!

Pinaceous
 
Last edited:

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
7,920
Office Version
2007
Platform
Windows
What range do you mean?
 

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
589
The previously posted
Code:
 MsgBox "Dates Within Range"
should be just be interpreted as
Code:
 MsgBox " Valid Dates"
from
Code:
 sDate1 = #10/1/2019#
  sDate2 = #9/30/2020#
.
 
Last edited:

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
7,920
Office Version
2007
Platform
Windows
Sorry, but I'm not understanding what your question is.
 

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
589
Hi Dante Amor,

I understand now. Sorry about that! The range I’m working with is in Range("B8:B66").
This is where I have the user list the date between:
Code:
 sDate1= #10/1/2019#,  sDate2 = #9/30/2020#

So, now how can I make the following code work?

Code:
[FONT=Calibri][SIZE=3][COLOR=#000000]Sub test()[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]  Dim sDate1 As Date, sDate2 As Date[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]  sDate1 = #10/1/2019#[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]  sDate2 = #9/30/2020#[/COLOR][/SIZE][/FONT]

[FONT=Calibri][SIZE=3][COLOR=#000000]  If Date >= sDate1And Date <= sDate2 Then[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]    MsgBox "DatesWithin Range"[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]  End If[/COLOR][/SIZE][/FONT]

[FONT=Calibri][SIZE=3][COLOR=#000000] If Date <>Empty Then[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]        MsgBox " NO Dates Listed "[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]  End If [/COLOR][/SIZE][/FONT]

[FONT=Calibri][SIZE=3][COLOR=#000000]End Sub[/COLOR][/SIZE][/FONT]
Thank you,
Pinaceous
 
Last edited:

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
7,920
Office Version
2007
Platform
Windows
Do you need a cycle to verify the dates you have in the range "B8:B66"?
 

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
589
I'm pretty sure I don't because I have Private Sub Workbook code that cycles through the data that the user provides.
 

Forum statistics

Threads
1,078,461
Messages
5,340,440
Members
399,375
Latest member
alwayssunny

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top