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

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
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,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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