Hello, Sorry in advance if I'm not using the correct terminology but hopefully I can explain it well enough. This is for some code I am still writing so don't have a complete code to post but I have copied what I think are the relevant bits.
I'm trying to get a formula added to a range by using a string variable based on an excel cell (I want multiple formula's and the ability to change them without hardcoding into the VBA).
Key bits I'm trying to get to work are:
The issue I have is:
If I enter
strFormula = "=IFERROR(VLOOKUP(A2," & strRange & "," & strHeaderColumn & ",0),"""")"
in the Immediate window it recognises it as a formula and strFormula shows as
=IFERROR(VLOOKUP(A2,,Sheet1!A:AP,2,0),"")
This works as expected when executing 'wks2.range("B2").Formula = strFormula'.
If I enter
"=IFERROR(VLOOKUP(A2," & strRange & "," & strHeaderColumn & ",0),"""")"
in wks2.Cells(intMasterRow, 5) then strFormula shows as
"=IFERROR(VLOOKUP(A2," & strRange & "," & strHeaderColumn & ",0),"""")"
when executing 'wks2.range("B2").Formula = strFormula' it adds this text in, instead of the expected formula.
The cell in wks2 is set as text and I've tried various variations of quote marks to try and get it to work with no luck.
Is there a way to get VBA to interpret the Excel Cell value as it would in the Immediate Window and get the correct formula entered? I'm guessing either I have the quotes set up wrong for what I'm doing and/or there is a function I can add to get it to work.
Hopefully the issue is clear, if not let me know.
many thanks
I'm trying to get a formula added to a range by using a string variable based on an excel cell (I want multiple formula's and the ability to change them without hardcoding into the VBA).
Key bits I'm trying to get to work are:
VBA Code:
Dim strFormula as String
strRange = Sheet1!A:AP
strHeaderColumn = 2
strFormula = wks1.Cells(intMasterRow, 5)
wks2.range("B2").Formula = strFormula
The issue I have is:
If I enter
strFormula = "=IFERROR(VLOOKUP(A2," & strRange & "," & strHeaderColumn & ",0),"""")"
in the Immediate window it recognises it as a formula and strFormula shows as
=IFERROR(VLOOKUP(A2,,Sheet1!A:AP,2,0),"")
This works as expected when executing 'wks2.range("B2").Formula = strFormula'.
If I enter
"=IFERROR(VLOOKUP(A2," & strRange & "," & strHeaderColumn & ",0),"""")"
in wks2.Cells(intMasterRow, 5) then strFormula shows as
"=IFERROR(VLOOKUP(A2," & strRange & "," & strHeaderColumn & ",0),"""")"
when executing 'wks2.range("B2").Formula = strFormula' it adds this text in, instead of the expected formula.
The cell in wks2 is set as text and I've tried various variations of quote marks to try and get it to work with no luck.
Is there a way to get VBA to interpret the Excel Cell value as it would in the Immediate Window and get the correct formula entered? I'm guessing either I have the quotes set up wrong for what I'm doing and/or there is a function I can add to get it to work.
Hopefully the issue is clear, if not let me know.
many thanks
Last edited by a moderator: