Help inserting formula in a Macro

RoccoM

New Member
Joined
Apr 22, 2019
Messages
19
Hi:
I have an existing formula that works well for me. I would like to use it in a macro.
So I would like to copy the formula in cell Q2.

First here is the formula
=IF(F2="y",(IF(((ISNUMBER(SEARCH("blade",E2))*AND(B2="Server"))),"Blade Server","Card")),(IF(((ISNUMBER(SEARCH("blade",E2))*AND(B2="Server"))),"Blade Enclosure",PROPER(B2))))

Here is what I have in terms of the code
Range("Q2").Formula = "=IF(F2="y",(IF(((ISNUMBER(SEARCH("blade",E2))*AND(B2="Server"))),"Blade Server","Card")),(IF(((ISNUMBER(SEARCH("blade",E2))*AND(B2="Server"))),"Blade Enclosure",PROPER(B2))))"

I get the following error, even before I try to run my macro.
Compile error:
Expected: end of statement

Range("Q2").Formula = "=IF(F2="y",(IF(((ISNUMBER(SEARCH("blade",E2))*AND(B2="Server"))),"Blade Server","Card")),(IF(((ISNUMBER(SEARCH("blade",E2))*AND(B2="Server"))),"Blade Enclosure",PROPER(B2))))"

How can I copy the above formula into cell Q2?
Thank you,
Rocco
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
This issue is that double-quotes are treated as Text Qualifiers when building your formulas in VBA, so it is causing issues.

The easiest thing to do is the following:
1. Turn on the Macro Recorder (from the Macro menu)
2. Go to cell Q2
3. Manually enter your formula in that cell:
Code:
[COLOR=#333333]=IF(F2="y",(IF(((ISNUMBER(SEARCH("blade",E2))*AND(B2="Server"))),"Blade Server","Card")),(IF(((ISNUMBER(SEARCH("blade",E2))*AND(B2="Server"))),"Blade Enclosure",PROPER(B2))))
4. Stop the Macro Recorder
5. View the VBA code you just recorded, and copy the formula portion to the spot you want it in your VBA code. This will give you the formula exactly the way that VBA needs it.

So, essentially, you are just letting the Macro Recorder do the work of writing the formula in VBA format for you!
[/COLOR]
 
Last edited:
Upvote 0
PERFECT! Worked like a charm!
I'm starting to get it :)
Thank you for the very quick reply.
Rocco
 
Upvote 0
You are welcome.
Glad I was able to help!

Yes, the Macro Recorder is a great little tool to get snippets of code. Just record yourself performing the step manually, and you will have VBA code to do it. Even us "experts" use it!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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