VBA Code To Find Specific Work And Insert Formula Next Cell

Shums

New Member
Joined
Jul 24, 2011
Messages
18
Hi All,

I am trying to calculate months remaining between two dates, I am trying with below code, but doesn't do anything...Kindly help....
Code:
Sub ChangeMonthsRemaining()
    Dim cl As Range
    Application.ScreenUpdating = False
    For Each cl In Range("$D$2:$D" & Range("$D65536").End(xlUp).Row)
    If UCase(cl) = "Months Remaining" Then
        cl.Offset(0, 1).Formula = "=MONTH($C$1-$D$1)"
    End If
    Next cl
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Code:
 If UCase(cl) = "Months Remaining" Then

will never be true. Try something like

Code:
 If LCase(cl) = "months remaining" Then
 
Upvote 0
One more thing, when I am trying

cl.Offset(0, 1).Formula = "=DATEDIF($D$1,$C$1,"M")" it gives me sintax error. End Statement at "M". Any idea????
 
Upvote 0
You need to double up the quotes inside the formula. Try

Code:
cl.Offset(0, 1).Formula = "=DATEDIF($D$1,$C$1,""M"")"
 
Upvote 0
Actually I wish this to work automated, I dont need to specify cell reference and input current worked month and the months remaining for current year. Do you have any other idea? Please guide me...
 
Upvote 0

Forum statistics

Threads
1,216,742
Messages
6,132,453
Members
449,729
Latest member
davelevnt

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