Hi,
I am new to writing macros and have written the following macro but it doesn't work properly.
I have four columns with dates and I need to find the number of month between the dates.
Example:
Between col C and D, but if column C is blank, then, between col B and D and if col B is blank, then A and D.
And if A,B and C are all blank then I need the result to be blank.
Sub Calc_Dates_Stat4()
Dim prev_date As Date
Dim curr_date As Date
Dim Months
Dim NA As Boolean
Range("r2").Select
Do Until IsEmpty(Range("o" & ActiveCell.Row))
NA = False
If IsEmpty(Range("d" & ActiveCell.Row)) Then
NA = True
Else
curr_date = Range("d" & ActiveCell.Row).Value
End If
If IsEmpty(Range("c" & ActiveCell.Row)) Then
If IsEmpty(Range("b" & ActiveCell.Row)) Then
NA = True
If IsEmpty(Range("a" & ActiveCell.Row)) Then
NA = True
Else: prev_date = Range("a" & ActiveCell.Row).Value
End If
Else: prev_date = Range("b" & ActiveCell.Row).Value
End If
Else
prev_date = Range("c" & ActiveCell.Row).Value
End If
If NA = False Then
ActiveCell.Value = MonthDiff(prev_date, curr_date)
End If
ActiveCell.Offset(1, 0).Select
Loop
End Sub
At the moment it is not calculating if Col B and Col C are both blank.
Can anyone please help?
Thanks
I am new to writing macros and have written the following macro but it doesn't work properly.
I have four columns with dates and I need to find the number of month between the dates.
Example:
Between col C and D, but if column C is blank, then, between col B and D and if col B is blank, then A and D.
And if A,B and C are all blank then I need the result to be blank.
Sub Calc_Dates_Stat4()
Dim prev_date As Date
Dim curr_date As Date
Dim Months
Dim NA As Boolean
Range("r2").Select
Do Until IsEmpty(Range("o" & ActiveCell.Row))
NA = False
If IsEmpty(Range("d" & ActiveCell.Row)) Then
NA = True
Else
curr_date = Range("d" & ActiveCell.Row).Value
End If
If IsEmpty(Range("c" & ActiveCell.Row)) Then
If IsEmpty(Range("b" & ActiveCell.Row)) Then
NA = True
If IsEmpty(Range("a" & ActiveCell.Row)) Then
NA = True
Else: prev_date = Range("a" & ActiveCell.Row).Value
End If
Else: prev_date = Range("b" & ActiveCell.Row).Value
End If
Else
prev_date = Range("c" & ActiveCell.Row).Value
End If
If NA = False Then
ActiveCell.Value = MonthDiff(prev_date, curr_date)
End If
ActiveCell.Offset(1, 0).Select
Loop
End Sub
At the moment it is not calculating if Col B and Col C are both blank.
Can anyone please help?
Thanks