Reducing the amount of code with Arrays

gareth1979

New Member
Joined
May 28, 2018
Messages
5
This is my code for checking the system clock and comparing it with the dates of the tax year

Code:
Sub dateesystem()
'decclare all variable to differnt type
'-------------------------------------------------
Dim StartDateMonth01 As Date
Dim StartDateMonth02 As Date
Dim StartDateMonth03 As Date
Dim StartDateMonth04 As Date
Dim StartDateMonth05 As Date
Dim StartDateMonth06 As Date
Dim StartDateMonth07 As Date
Dim StartDateMonth08 As Date
Dim StartDateMonth09 As Date
Dim StartDateMonth10 As Date
Dim StartDateMonth11 As Date
Dim StartDateMonth12 As Date
Dim EndDateMonth01 As Date
Dim EndDateMonth02 As Date
Dim EndDateMonth03 As Date
Dim EndDateMonth04 As Date
Dim EndDateMonth05 As Date
Dim EndDateMonth06 As Date
Dim EndDateMonth07 As Date
Dim EndDateMonth08 As Date
Dim EndDateMonth09 As Date
Dim EndDateMonth10 As Date
Dim EndDateMonth11 As Date
Dim EndDateMonth12 As Date
Dim o As Date
Dim counter As Integer
counter = 0
'---------------------------------------------------------
'declare variables source and location
'---------------------------------------------------------
StartDateMonth01 = Worksheets("Sheet1").Range("b3").Value
EndDateMonth01 = Worksheets("Sheet1").Range("c3").Value
StartDateMonth02 = Worksheets("Sheet1").Range("b4").Value
EndDateMonth02 = Worksheets("Sheet1").Range("c4").Value
StartDateMonth03 = Worksheets("Sheet1").Range("b5").Value
EndDateMonth03 = Worksheets("Sheet1").Range("c5").Value
StartDateMonth04 = Worksheets("Sheet1").Range("b6").Value
EndDateMonth04 = Worksheets("Sheet1").Range("c6").Value
StartDateMonth05 = Worksheets("Sheet1").Range("b7").Value
EndDateMonth05 = Worksheets("Sheet1").Range("c7").Value
StartDateMonth06 = Worksheets("Sheet1").Range("b8").Value
EndDateMonth06 = Worksheets("Sheet1").Range("c8").Value
StartDateMonth07 = Worksheets("Sheet1").Range("b9").Value
EndDateMonth07 = Worksheets("Sheet1").Range("c9").Value
StartDateMonth08 = Worksheets("Sheet1").Range("b10").Value
EndDateMonth08 = Worksheets("Sheet1").Range("c10").Value
StartDateMonth09 = Worksheets("Sheet1").Range("b11").Value
EndDateMonth09 = Worksheets("Sheet1").Range("c11").Value
StartDateMonth10 = Worksheets("Sheet1").Range("b12").Value
EndDateMonth10 = Worksheets("Sheet1").Range("c12").Value
StartDateMonth11 = Worksheets("Sheet1").Range("b13").Value
EndDateMonth11 = Worksheets("Sheet1").Range("c13").Value
StartDateMonth12 = Worksheets("Sheet1").Range("b14").Value
EndDateMonth12 = Worksheets("Sheet1").Range("c14").Value
o = Now()
'----------------------------------------------------------
'If true or false
'----------------------------------------------------------
If (o >= StartDateMonth01 And o <= EndDateMonth01) Then
counter = 1
MsgBox counter
Else
If (o >= StartDateMonth02 And o <= EndDateMonth02) Then
counter = 2
MsgBox counter
Else
If (o >= StartDateMonth03 And o <= EndDateMonth03) Then
counter = 3
MsgBox counter
Else
If (o >= StartDateMonth04 And o <= EndDateMonth04) Then
counter = 4
MsgBox counter
If (o >= StartDateMonth05 And o <= EndDateMonth05) Then
counter = 5
MsgBox counter
Else
If (o >= StartDateMonth06 And o <= EndDateMonth06) Then
counter = 6
MsgBox counter
Else
If (o >= StartDateMonth07 And o <= EndDateMonth07) Then
counter = 7
MsgBox counter
Else
If (o >= StartDateMonth08 And o <= EndDateMonth08) Then
counter = 8
MsgBox counter
If (o >= StartDateMonth09 And o <= EndDateMonth09) Then
counter = 9
MsgBox counter
Else
If (o >= StartDateMonth10 And o <= EndDateMonth10) Then
counter = 10
MsgBox counter
Else
If (o >= StartDateMonth11 And o <= EndDateMonth11) Then
counter = 11
MsgBox counter
Else
If (o >= StartDateMonth12 And o <= EndDateMonth12) Then
counter = 12
MsgBox counter
Else
MsgBox "its not working"
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End Sub

However I want to try and reduce the script by using and array and creating a loop to do check the system date that way, below is what I have tried doing however I can seem to get anywhere can someone help or have any suggestions please
Code:
Sub chDates()
Dim myDatestart As Variant
Dim myDateEnd As Variant
Dim c As Long
Dim i As Variant
myDatestart = Range("a1:a5").Value
myDateEnd = Range("b1:b5").Value
 i = Now()
 c = 0
 While i >= ((UBound(myDatestart) And i >= (UBound(myDateEnd))))
 
 c = c + 1
 If i >= ((UBound(myDatestart) And i <= (UBound(myDateEnd)))) Then
 If c = c < 6 Then
 MsgBox c
Else
 End If
 End If
 
Wend
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Try the below

Code:
Sub Date_System()

Dim StartDate As Date, EndDate As Date, Counter As Integer, lRow As Long, Ws As Worksheet

Set Ws = Worksheets("Sheet1")
lRow = Ws.Range("B" & Rows.Count).End(xlUp).Row

For x = 2 To lRow
    StartDate = Ws.Cells(x, 2): EndDate = Ws.Cells(x, 3)
    If Date >= StartDate And Date <= EndDate Then
        Counter = x - 1
        MsgBox Counter
    End If
Next x

End Sub
 
Upvote 0
Another option
Code:
Sub chDates()
   Dim myDates As Variant
   Dim Mnth As Long
   Dim i As Long, j As Long
   myDates = Range("B3:C14").Value
   For i = 1 To UBound(myDates)
      If Date >= myDates(i, 1) And Date <= myDates(i, 2) Then
         Mnth = i
         Exit For
      End If
   Next i
   MsgBox Mnth
End Sub
 
Last edited:
Upvote 0
Thanks mse200, however this doesn't seem to work

Here is my excel spread sheet

AB
01/06/201830/06/2018
01/07/201830/07/2018
01/08/201830/08/2018
01/09/201830/09/2018

<tbody>
</tbody>


I want an array to collect the dates in column A1 - A5 and another Array to collect the dates B1-B5, then a loop will check the computer date against each of these array dates and if the value is true then message box should display the month of the tax year and if not carry on till it does, hope this helps (please be advised these dates and just random at the moment )
 
Upvote 0
Another option
Code:
Sub chDates()
   Dim myDates As Variant
   Dim Mnth As Long
   Dim i As Long, j As Long
   myDates = Range("B3:C14").Value
   For i = 1 To UBound(myDates)
      If Date >= myDates(i, 1) And Date <= myDates(i, 2) Then
         Mnth = i
         Exit For
      End If
   Next i
   MsgBox Mnth
End Sub

Thanks fluff,Tried this and it doesn't seen to work
 
Upvote 0
I based that on your original code so it's looking in B3:C14 for dates set like this


Excel 2013 32 bit
ABC
1
2
301/01/201831/01/2018
401/02/201828/02/2018
501/03/201831/03/2018
601/04/201830/04/2018
701/05/201831/05/2018
801/06/201830/06/2018
901/07/201831/07/2018
1001/08/201831/08/2018
1101/09/201830/09/2018
1201/10/201831/10/2018
1301/11/201830/11/2018
1401/12/201831/12/2018
Sheet1
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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