I need help with this VBA. Thanks in advance.
I want to select a range (of values, the whole column) for the values: FirstDate, EndDate and Number. My VBA:
Sub DateTest()
Dim FirstDate As Date ' Declare variables.
Dim IntervalType As String
Dim Number As Integer
Dim EndDate As Date
Dim TempDate As Date
Dim i As Integer
IntervalType = "m" ' "m" specifies MONTHS as interval.
FirstDate = Cells(1, 1).Value
EndDate = Cells(1, 2).Value
Number = Cells(1, 3).Value ' "Number" For the syntax DateAdd.
' If the number is not greater than zero an infinite loop will happen.
If Number <= 0 Then
MsgBox "Number needs to be greater than 0", vbCritical
Exit Sub
End If
i = 1
Do Until TempDate = EndDate
If i <= 1 Then
TempDate = DateAdd(IntervalType, Number, FirstDate)
Else
TempDate = DateAdd(IntervalType, Number, TempDate)
End If
i = i + 1
Debug.Print i
Loop
Range("D1").Value = i - 1
End Sub
As I wrote before I want to run my Macro not only for the 1st cell (currently the macro works fine for the value (1,1) (1,2) (1,3)), as you can see above for FirstDate, EndDate and Number I wan to use for all dates in: Column1, Column2, Column3
I made some changes, for example for EndDate:
EndDate = Format(.Cells(lRow, 2).Value)
is not working, take the vale for 1 cell and the rest of the values in the column are ignored.
I tried:
FirstDate = Range("A1:A20").Select EndDate = Range("B1:B20").Select Number = Range("C1:C20").Select
But I get: "Number needs to be greater than 0"