Write cell formula with IF condition -- is unequal to and equal to




I will write a formula in Excel with WRITE CELL.
It is a formula “=IF(AX”+cntRow.ToString+"<>"+" “+”,2,ROUND((AG"+cntRow.ToString+")))"
I think it doesn’t work because there is a <> in it. If I do the same formula with = it won’t work either.

why is this formula not working?
cnt.Row.ToString is counting the rows.

This is the error:


Hope someone can help me soon with this problem



@math check the value of cntRow. It may have intial value zero. change intial value to one.


@Manjuts90 The default of cntRow is already set on 2. cntRow starts counting on number 3 in my code. It is never zero.


This is still not the answer.

Anyone else who can solve this for me?


unless I am mistaken you are trying to check if AX is empty - like =IF(AX3="";)?

the way you put it would result in an excel error as it would compare AX to space

maybe try

or if you really want to check if it is a single space character
“=IF(AX”+cntRow.ToString+"<>"+"" ““+”,2,ROUND((AG”+cntRow.ToString+")))"

VBA code for double quote is CHR(34), that might be helpful too

added: I mean “bla” +CHR(34)+CHR(34)+“bla” gives you a srting of bla""bla…


@AlexJank makes a good point.
If you are checking a cell with a string, then the formula should have quotes in it, so you need to embed the quotes in your formula so it can correctly have " " as part of the formula.
(ie +cntRow.ToString+"<>""" “”"" ) or something like that

A good way to check your formula string is to output it with Write Line or Message Box, so you can see what you are inputting into the cell beforehand.


@claytonM and @AlexJank Thanks a lot.
It works fine now!