Formula in VBA

Chissa

Board Regular
Joined
Sep 27, 2011
Messages
66
Hi there,

Can I type normal excel formulas in to VBA? I have the below formula but I need to incorporate it in to my VBA code:

IF(AND(D3>0,D3<999),0&D3,IF(D3>999,D3,IF(D3=0,"0000")))

I so far have written this (with the help from Robert earlier),

Dim LastRow2 As Long
LastRow2 = Range("E" & Rows.Count).End(xlUp).Row

Range("E3:E" & LastRow2).Formula = "=IF(AND(D3>0,D3<999),0&D3,IF(D3>999,D3,IF(D3=0,"0000")))"

But it brings up an error message saying "Compile Error Syntax Error" and I have no idea why.

Hope you can help me

Chissa
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
You need double quotes if it is housed within quotes:

Code:
"=IF(AND(D3>0,D3<999),0&D3,IF(D3>999,D3,IF(D3=0,[B][COLOR="red"]"[/COLOR][/B]"0000"[COLOR="Red"][B]"[/B][/COLOR])))"
 
Upvote 0
Hi there,

Can I type normal excel formulas in to VBA? I have the below formula but I need to incorporate it in to my VBA code:

IF(AND(D3>0,D3<999),0&D3,IF(D3>999,D3,IF(D3=0,"0000")))

I so far have written this (with the help from Robert earlier),

Dim LastRow2 As Long
LastRow2 = Range("E" & Rows.Count).End(xlUp).Row

Range("E3:E" & LastRow2).Formula = "=IF(AND(D3>0,D3<999),0&D3,IF(D3>999,D3,IF(D3=0,"0000")))"

But it brings up an error message saying "Compile Error Syntax Error" and I have no idea why.

Hope you can help me

Chissa

try this

Range("E3:E10" & LastRow2).xlFormula = "=IF(AND(D3>0,D3<999),0&D3,IF(D3>999,D3,IF(D3=0,0,false)))"
 
Upvote 0

Forum statistics

Threads
1,224,559
Messages
6,179,513
Members
452,921
Latest member
BBQKING

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