Puzzled by VBA formula

Big Lar

Well-known Member
Joined
May 19, 2002
Messages
557
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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
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

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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