VBA Sub BetweenDates

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,124
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I'm working with the following sub and am wondering if there is a way to extend this range to include its column?

Please help with your posted suggestions & thank you!

Code:
Sub BTWDates()

Dim x As Date
Dim sDate1 As Date
Dim sDate2 As Date
sDate1 = #10/1/2019#                
sDate2 = #9/30/2020#                

x = ActiveSheet.Range("B8:B66")

Select Case x
  Case sDate1 To sDate2
  
        MsgBox "Within Date Range"
       
            
    Application.Cursor = xlDefault  'on completion / error
       
  Case Else
        
        MsgBox "Out of Date Specified Range"
End Select

End Sub

For example, when I do this:

Code:
x = ActiveSheet.Range("B8:B66")

in lieu of:

Code:
x = ActiveSheet.Range("B8")

it pops me with an error.

Please help me to extend the range of this sub.

Thank you!
pinaceous
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
How about:
Code:
Sub BTWDates()

    Dim x As Variant, d As Variant
    Dim sDate1 As Date
    Dim sDate2 As Date
    sDate1 = #10/1/2019#
    sDate2 = #9/30/2020#
    
    x = ActiveSheet.Range("B8:B66")
    
    For Each d In x
        Select Case d
            Case sDate1 To sDate2
                MsgBox d & " Within Date Range"
                Application.Cursor = xlDefault  'on completion / error
            Case Else
                MsgBox d & " Out of Date Specified Range"
        End Select
    Next

End Sub
 
Upvote 0
Hi John_w!

Your code which extends the range
Code:
  x = ActiveSheet.Range("B8:B66")
works perfectly!

However, the code does not exclude any of the bank cells in the specified range of that column.

Where it pops the
Code:
 MsgBox d & " Out of Date Specified Range"
for each blank cell provided in that specified range.

So, it there a way to exclude the blank cells from the code, where it only runs on the cells with a date format?

Many thanks in advance!
pinaceous


<a href="https://www.mrexcel.com/forum/profile.php?do=addlist&userlist=buddy&u=84512" target="_blank"><a href="https://www.mrexcel.com/forum/members/john_w.html" target="_blank"><a href="https://www.mrexcel.com/forum/search.php?do=finduser&userid=84512&contenttype=vBForum_Post&showposts=1" target="_blank"><a href="https://www.mrexcel.com/forum/private.php?do=newpm&u=84512" target="_blank"><a href="https://www.mrexcel.com/forum/profile.php?do=addlist&userlist=buddy&u=84512" target="_blank">


How about:
Code:
Sub BTWDates()

     Dim x As Variant, d As Variant
     Dim sDate1 As Date
     Dim sDate2 As Date
     sDate1 = #10/1/2019#
     sDate2 = #9/30/2020#
     
     x = ActiveSheet.Range("B8:B66")
     
     For Each d In x
        Select Case d
             Case sDate1 To sDate2
                 MsgBox d & " Within Date Range"
                 Application.Cursor = xlDefault  'on completion / error
             Case Else
                 MsgBox d & " Out of Date Specified Range"
         End Select
     Next

 End Sub
 
Last edited:
Upvote 0
Add an If ... End If block:
Code:
        If d <> Empty Then

        End If
around the Select ... End Select block.
 
Upvote 0
Add an If ... End If block:
Code:
        If d <> Empty Then

        End If
around the Select ... End Select block.

Hi John,

I have another question, in case I apply the concept in the following way, which is how do I apply the code to act on more than one sheet in your code.

For example, if I want your code to work on sheets 1, 6, 8, & 9; in lieu of "active".

This would help, if you can specify these sheets in your code.

Thank you!
pinaceous
 
Upvote 0
Try this:
Code:
Sub BTWDates2()

    Dim i As Variant
    Dim cell As Range
    Dim sDate1 As Date
    Dim sDate2 As Date
    sDate1 = #10/1/2019#
    sDate2 = #9/30/2020#
    
    For Each i In Array(1, 6, 8, 9)
        For Each cell In Worksheets(i).Range("B8:B66")
            If cell.Value <> Empty Then
                Select Case cell.Value
                    Case sDate1 To sDate2
                        MsgBox cell.Value & " Within Date Range", Title:=Worksheets(i).Name & " " & cell.Address
                        Application.Cursor = xlDefault  'on completion / error
                    Case Else
                        MsgBox cell.Value & " Out of Date Specified Range", Title:=Worksheets(i).Name & " " & cell.Address
                End Select
            End If
        Next
    Next

 End Sub
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

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