I have faced a strange thing in Excel VBA coding.
I have a sheet full of formulas, each refers to the cells in other sheets.
This is a sample formula in cell A1, for example:
=IF('General Inputs & Summary'!B6="","",'General Inputs & Summary'!B6)
I want to dynamically change the old tab names in formulas, with new tab names.
When I try this code:
oldStr = "'General Inputs & Summary'"
newStr = "'test'"
Range("A1").Formula = Replace(Range("A1").Formula, oldStr, newStr)
it results in:
=IF(test!B6="","",test!B6)
instead of:
=IF('test'!B6="","",'test'!B6)
You see, single Quotes are automatically removed, so the new formula fails!
Any solutions for this, please?
test
does not contain any spaces and quotes are only needed when there are spaces in your worksheet name. Your formula might fail because of other reasons but not because of the quotes here. Can you be more specific what "fails" mean? What error does it show? Is there a sheet calledtest
? Is calculation turned toxlAutomatic
? If not run calculate after changing the formula. – Pᴇʜ