Inserting a string in a macro line to create a formula for a coulmn

Bill_Biggs

Well-known Member
Joined
Feb 6, 2007
Messages
1,216
I have a column that a macro create selects to insert a formula in. The formula is like this:

=IF(OR(AL2={variable}),"Yes","No")

It places this fomula in AM2 but the column can vary so I am having to create a formula to plug in to the column. So This is the line from the macro that is supposed to do that:

ActiveCell.FormulaR1C1 = _
"=IF(OR(RC[-1]={" & K & "}),""Yes"",""No"")"


Now in the {} I want to place a string variable, K, that give this fomula its value seeking objective.

However, when ever I try to run it, this string errs out with an app or objects error. How should I be writing this so that the formula can be placed with the particular string I need it to find?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
What does K contain? This worked for me:

Code:
Sub Test()
    Dim K
    K = """a"",""b"""
    ActiveCell.FormulaR1C1 = _
        "=IF(OR(RC[-1]={" & K & "}),""Yes"",""No"")"
End Sub
 

Bill_Biggs

Well-known Member
Joined
Feb 6, 2007
Messages
1,216
I missed a set of quotation marks in creating K. I saw how you put them in and realized my error. Thanks,
 

Watch MrExcel Video

Forum statistics

Threads
1,129,685
Messages
5,637,809
Members
416,983
Latest member
LessThanAverageUser

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
Top