VBA If statement

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,113
Office Version
  1. 365
Platform
  1. Windows
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
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I'll try. Thank you for asking.

I have a sheet where the user inputs a date between sDate1& sDate2 into Column B in the Range of (“B6:B66”).


I want a Sub to be created, where if run upon a blank sheet (where no dates are present the Column B in the Range of (“B6:B66”)) the code will produce a message box to the effect of MsgBox " NO Dates Listed".

If there are dates posted between sDate1 & sDate2 in Column B in the Range of (“B6:B66”) a message box will produce a message box to the effect of MsgBox " Valid Dates Listed ".

If there are both blank entries and dates posted between sDate1& sDate2 in Column B in the Range of (“B6:B66”) a message box will produce a message box to the effect of MsgBox " Valid Dates Listed " and will override the MsgBox " NO Dates Listed " which will not appear.


At this point I am not concerned about any dates that might pop up outside of sDate1 & sDate2 in Column B in the Range of (“B6:B66”).
 
Last edited:
Upvote 0
I'm sorry but I still don't clearly understand what the request is.


You could explain with an example each of the events you want to validate.
 
Upvote 0
Column B
Column C
Column D
Row 6
11/13/19
Row 7
Row 8
...
Row 66

<tbody>
</tbody>


Date entry of 11/13/19 is between sDate1 (10/1/19) & sDate2 (9/30/20) in Column B and is in the Range of (“B6:B66”) a message box will produce a message box to the effect of MsgBox " Valid Dates Listed ".



Column B
Column C
Column D
Row 6
Row 7
Row 8
...
Row 66

<tbody>
</tbody>


A blank sheet (where no dates are present the Column B inthe Range of (“B6:B66”)) the code will produce a message box to the effect of MsgBox" NO Dates Listed ".


At this point, I am not concerned about any dates that might pop up outside of sDate1 & sDate2 in Column B in the Range of (“B6:B66”).

I hope this helps explain what I would like.

Thanks again Dante Amor for considering my dilemma.

R/
pinaceous
 
Last edited:
Upvote 0
I still don't understand very well what you want.


Let's start with the following.


Code:
Sub test1()
  Dim wCount As Variant, c As Range
  Dim sDate1 As Date, sDate2 As Date
  sDate1 = #10/1/2019#
  sDate2 = #9/30/2020#
  wCount = WorksheetFunction.Count(Range("B6:B66"))
  If wCount = 0 Then
    MsgBox "NO Dates Listed"
  Else
    For Each c In Range("B6:B66")
      If c <> "" Then
        If IsDate(c) Then
          If sDate1 <= c And c <= sDate2 Then
            MsgBox "Valid Dates Listed"
          Else
          
          End If
        End If
      End If
    Next
  End If
End Sub
 
Upvote 0
Hi Dante Amor,

I do like your code and it seems like you understand it very well.

For your code posted#15, if I have more than one entry it provides a message box for every entry.

Could you kindly revise your code to provided only one message box for multiple entries?

For example,

Column B
Column C
Column D
row 6
11/10/19
row 7
11/11/19
row 8
11/12/19

...
row 66

<tbody>
</tbody>



If I have three valid entries as can be seen here, 11/10/19, 11/11/19, & 11/12/19, can you have the message box of
Code:
 MsgBox "Valid Dates Listed"
please provide only one message box pop-up for multiple entries?

Thank you,
pinaceous
 
Upvote 0
After msgbox "valid dates" line add this line:

Exit For
 
Last edited:
Upvote 0
Thank you Dante Amor for helping me with my requests even though it was not traditional and seemed strange. I just wanted to say thank you! ?
 
Upvote 0
Hi DanteAmor,

If I place the Exit For after the MsgBox I get an error.


VBA Code:
Sub test1()
  Dim wCount As Variant, c As Range
  Dim sDate1 As Date, sDate2 As Date
  sDate1 = #10/1/2019#
  sDate2 = #9/30/2020#
  wCount = WorksheetFunction.Count(Range("B6:B66"))
  If wCount = 0 Then
    MsgBox "NO Dates Listed"
    Exit For
    'End Sub
  Else
    For Each c In Range("B6:B66")
      If c <> "" Then
        If IsDate(c) Then
          If sDate1 <= c And c <= sDate2 Then
            MsgBox "Valid Dates Listed"
          Else
         
          End If
        End If
      End If
    Next
  End If
End Sub



Any suggestions?

Thank you!
Pinaceous
 
Upvote 0

Forum statistics

Threads
1,215,426
Messages
6,124,828
Members
449,190
Latest member
rscraig11

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