Writing a formula in VBA that checks for blank cells

Gaetan12345

New Member
Joined
Feb 8, 2018
Messages
9
Good morning,

i'm writing a simple formula that substract 2 values if a cell is not blank & Excel keeps-on guiving me errors...this seems simple, but i'm not sure where the error is located?

Dim FormuleDonnee As String

FormuleDonnee = "=if($G21<>"";$J21-$G21;"")"

Range("o21").formula = FormuleDonnee
Range("o21").AutoFill Destination:=Range("o21:eek:400")

When i use MSGBOX to find out what is the result of this formula, it seems to take away one of the "...on other examples, they say to write 4 ". Still, the error comes back.


Thank's for any help
 

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.
I finally used different syntax & the formula ias has expected...

FormuleDonnee = "=si(G21<>" & Chr(34) & Chr(34) & ",J21 - G21," & Chr(34) & Chr(34) & ")"

Now, i just have to find a way to execute this formula; the #name ? appears. Once i go on the cell, & press ENTER, it refreshes as expected.
 
Upvote 0
FormuleDonnee = "=if($G21<>"";$J21-$G21;"")"
You must double up quote marks inside of a text string constant (two internal quote marks stand in for a single quote mark on output), so your above formula should read...

FormuleDonnee = "=if($G21<>"""";$J21-$G21;"""")"
 
Upvote 0
Thank's Rick, the solution you provided works (the same as if i used the chr(34)).

The only thing concerns the refresh; i always get a #NAME ?. If i go to the formula bar, go at the end of my formula & press enter, it refreshes correctly. I've checked the Automatic calculation & still, i have to go in the formula bar & press enter??

Thank's for your reply.
 
Upvote 0
In VBA you should use , (comma) as argument separator not ; (semicolon)

See if this works

Code:
Dim FormuleDonnee As String

FormuleDonnee = "=if($G21<>""""[COLOR=#ff0000],[/COLOR]$J21-$G21[COLOR=#ff0000],[/COLOR]"""")"
 
Range("o21").formula = FormuleDonnee
Range("o21").AutoFill Destination:=Range("o21:o400")

M.
 
Upvote 0
The only thing concerns the refresh; i always get a [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=NAME]#NAME [/URL] ?. If i go to the formula bar, go at the end of my formula & press enter, it refreshes correctly. I've checked the Automatic calculation & still, i have to go in the formula bar & press enter??
I notice in your first message, you used the function name IF and in your second message you changed it to SI... one of them is incorrect and if you use that incorrect one, it will generate a #NAME ! error. Is that, perhaps, the source of your problem?
 
Upvote 0
I'm using a french version of excel. I tried the IF statement & for unknown reason, it does it's translation & brings up the formula SI, but this time, working as anticipated in the first time. (finally)

Thank's for you reply
 
Upvote 0
I'm using a french version of excel. I tried the IF statement & for unknown reason, it does it's translation & brings up the formula SI, but this time, working as anticipated in the first time. (finally)

Thank's for you reply

FYI: As in your Excel (French version) the IF (English) function is SI you can use FormulaLocal in VBA - using exactly the same formula that you use in a worksheet cell (doubling the "" inside the formula string)

Something like

Code:
Dim FormuleDonnee As String

FormuleDonnee = "=SI($G21<>"""";$J21-$G21;"""")"

Range("O21").[COLOR=#0000ff]FormulaLocal[/COLOR] = FormuleDonnee
Range("o21").AutoFill Destination:=Range("o21:o400")

M.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,431
Members
448,961
Latest member
nzskater

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