Puzzled by VBA formula

Big Lar

Well-known Member
Joined
May 19, 2002
Messages
554
Code:
Dim Column_L_L As Long
Set ws = Sheets("Members")
    Column_L_L = ws.Range("A" & Rows.Count).End(xlUp).Row
       ws.Range("L2:L" & ws.Range("A" & Rows.Count).End(xlUp).Row).Formula = _
"=YEARFRAC(K2, TODAY(), 1)"
 
        ws.Calculate

This bit of code creates an issue in my project.
If cells in ColumnK are blank, adjoining cell L values = 119. This producing undesired results later in the project.

In an attempt to resolve this issue, I’ve revised the formula to:
“=IF(K2="”,"”,(YEARFRAC(K2,TODAY(),1)))”

In Runtime, the formula populates as:
“=IF(K2=",",(YEARFRAC(K2,TODAY(),1)))”

This is puzzling. What am I doing wrong?
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
What I would do here to get the right formula,

Record Macro
Click on cell with formula and enter
Stop macro and view code

Then copy the formula part into your macro
 
Upvote 0
Try this.
Code:
Dim Column_L_L As Long
 
    Set ws = Sheets("Members")

    Column_L_L = ws.Range("A" & Rows.Count).End(xlUp).Row

    ws.Range("L2:L" & ws.Range("A" & Rows.Count).End(xlUp).Row).Formula = "=IF(K2="""","""",YEARFRAC(K2,TODAY(),1))"
 
    ws.Calculate
 
Upvote 0
In an attempt to resolve this issue, I’ve revised the formula to:
“=IF(K2="”,"”,(YEARFRAC(K2,TODAY(),1)))”

In Runtime, the formula populates as:
“=IF(K2=",",(YEARFRAC(K2,TODAY(),1)))”

This is puzzling. What am I doing wrong?
Quote marks inside of the quote marks that define a text constant must be doubled up (otherwise VB cannot tell if you are trying to end the text constant early). Try it this way and it should work...

“=IF(K2="”"","""”,YEARFRAC(K2,TODAY(),1))”
 
Last edited:
Upvote 0
Norie and Rick!!!
To the rescue, again!

Thanks for the explanation.
It does indeed work.
 
Upvote 0

Forum statistics

Threads
1,213,522
Messages
6,114,112
Members
448,549
Latest member
brianhfield

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