Setting a formula in macro basic

Morning,
Trying to set a formula in a specific cell, but it keeps recognising it as just text…

Using the following code:

sheet.getCellByPosition(4,4).String = date()
sheet.getCellByPosition(4,5).String = InvoiceNum
sheet.getCellByPosition(3,16).String = "=IF(B17="";"";1)"

but it keeps coming up a text, and (obviously) not working.
Others work fine, and looking at the Watch results, looks like it should be ok (ie Formula = “=IF(H5=”";"";15)".

Anyone?
Thanks.

You need to set it as a formula:

sheet.getCellByPosition(3,16).setFormula("=IF(B17="";"";1)")

Edit: Sorry - forgot a ) - this time for sure

If this answer resolves your question please tick the :heavy_check_mark: (upper left area of answer).

That does ‘seem’ to work, but now have another issue. When I do that, it successfully puts a formula in the cell, but now the formula just won’t work…?
ie puts the formula in the cell, but when I populate B17, it doesn’t place a “1” in the cell, just stays blank. It appears to get rid of 1 of each of the pairs of inverted commas. ie formula becomes:
=IF(B17=";";1) (with no error…)

Hi

The formula is a string, opened and closed by a quote. In Basic quotes contained within must be doubled

.setFormula("=IF(B17="""";"""";1)")

Regards

Excellent! Yes, that works, but… it keeps reformatting the cell as Boolean… :frowning:

@PYS - Thank you for input; I should have caught that.

hornetster - Because of entering the original formula with single quotes, a Boolean Value resulted. You should be able to correct this with a right click on the cell and selecting Clear Direct Formatting.

Thanks a heap! All working now…