Error in macro

Aurora118

New Member
Joined
Oct 10, 2007
Messages
42
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
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
You can do this with native excel formulas (If you have 2003, you'll need to enable the Analysis Toolpak add-in):

<b>Excel 2007</b><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #A6AAB6"><colgroup><col width="25px" style="background-color: #E0E0F0" /><col /><col /><col /><col /><col /></colgroup><thead><tr style=" background-color: #E0E0F0;text-align: center;color: #161120"><th></th><th>A</th><th>B</th><th>C</th><th>D</th><th>E</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">1</td><td style="text-align: right;;">1/1/2001</td><td style="text-align: right;;">3/1/2001</td><td style="text-align: right;;"></td><td style="text-align: right;;">9/2/2002</td><td style="text-align: right;;">18</td></tr></tbody></table><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #A6AAB6"><thead><tr style="background-color: #E0E0F0;text-align: center;color: #161120"><th><b>Sheet1</b></th></tr></td></thead></table><br /><br /><table width="85%" cellpadding="2.5px" rules="all" style=";border: 2px solid black;border-collapse:collapse;padding: 0.4em;background-color: #FFFFFF" ><tr><td style="padding:6px" ><b>Worksheet Formulas</b><table cellpadding="2.5px" width="100%" rules="all" style="border: 1px solid;text-align:center;background-color: #FFFFFF;border-collapse: collapse; border-color: #A6AAB6"><thead><tr style=" background-color: #E0E0F0;color: #161120"><th width="10px">Cell</th><th style="text-align:left;padding-left:5px;">Formula</th></tr></thead><tbody><tr><th width="10px" style=" background-color: #E0E0F0;color: #161120">E1</th><td style="text-align:left">=DATEDIF(<font color="Blue">LOOKUP(<font color="Red">9.99E+307,A1:C1</font>),D1,"m"</font>)</td></tr></tbody></table></td></tr></table><br />
 
Upvote 0
Excel 2003
I clicked Tools, Add Ins, Analysis Tool Pak and OK
then I copied your formula and it comes up with #N/A
 
Upvote 0
What values do you have in cells A1, B1, C1, and D1?
 
Upvote 0
I assumed your highest date would be in Column D.

Try:

<b>Excel 2007</b><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #A6AAB6"><colgroup><col width="25px" style="background-color: #E0E0F0" /><col /><col /><col /><col /><col /></colgroup><thead><tr style=" background-color: #E0E0F0;text-align: center;color: #161120"><th></th><th>A</th><th>B</th><th>C</th><th>D</th><th>E</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">1</td><td style="text-align: right;;">1/2/2007</td><td style="text-align: right;;"></td><td style="text-align: right;;">5/10/2008</td><td style="text-align: right;;">1/6/2007</td><td style="text-align: right;;">16</td></tr></tbody></table><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #A6AAB6"><thead><tr style="background-color: #E0E0F0;text-align: center;color: #161120"><th><b>Sheet1</b></th></tr></td></thead></table><br /><br /><table width="85%" cellpadding="2.5px" rules="all" style=";border: 2px solid black;border-collapse:collapse;padding: 0.4em;background-color: #FFFFFF" ><tr><td style="padding:6px" ><b>Worksheet Formulas</b><table cellpadding="2.5px" width="100%" rules="all" style="border: 1px solid;text-align:center;background-color: #FFFFFF;border-collapse: collapse; border-color: #A6AAB6"><thead><tr style=" background-color: #E0E0F0;color: #161120"><th width="10px">Cell</th><th style="text-align:left;padding-left:5px;">Formula</th></tr></thead><tbody><tr><th width="10px" style=" background-color: #E0E0F0;color: #161120">E1</th><td style="text-align:left">=DATEDIF(<font color="Blue">MIN(<font color="Red">LOOKUP(<font color="Green">9.99E+307,A1:C1</font>),D1</font>),MAX(<font color="Red">LOOKUP(<font color="Green">9.99E+307,A1:C1</font>),D1</font>),"m"</font>)</td></tr></tbody></table></td></tr></table><br />
 
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,304
Members
452,904
Latest member
CodeMasterX

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