0

I am wrestling with the syntax of this one line of some code. the formula with "IfError" seems to have the wrong syntax. I believe I have the quotes in the right places.

    Dim j As Integer
    Dim MidPointE As String
    Dim Dist As String
    Dim Allocation As String

    MidPointE = "AM"
    Dist = "AN"
    Allocation = "AO"

   for J= 1 to 300
     Cells(j, Dist).Formula = "=IFERROR(" & MidPointE & j & " / " & MidPointE &  CustomerLast & ", "")"
    Cells(j, Allocation).Formula = "=" & Allocation & j & "* S" & CustomerLast

     Next J
3
  • 3
    & ", "")" at the end should be & ", """")" Commented Nov 15, 2017 at 23:21
  • 1
    FWIW - Cells(j, Allocation).Formula = "=" & Allocation & j & "* S" & CustomerLast (after you assign a value to CustomerLast) is going to cause circular reference errors. Commented Nov 15, 2017 at 23:25
  • Thank you! I had tried that before but getting confirmation suddenly made it work! Commented Nov 16, 2017 at 15:30

3 Answers 3

1

You can also assign multiple formulas at the same time:

Range("AN1:AN300").Formula = "=IFERROR(AM1 / AM$" &  CustomerLast & ", """")"
Range("AO1:AO300").Formula = "=AO1 * S$" & CustomerLast

I also recommend looking into Excel Tables and Structured References

To avoid the circular reference issue, you can calculate the formulas and assign the values directly:

Range("AN1:AN300") = Evaluate("IFERROR(AM1:AM300 / AM$" &  CustomerLast & ", """")")
Range("AO1:AO300") = Evaluate("INDEX(AO1:AO300 * S$" & CustomerLast & ",)")
Sign up to request clarification or add additional context in comments.

1 Comment

And that AO cell is still going to cause a circular reference - but only the OP can solve that.
1

Try this:

 Cells(j, Dist).Formula = "=IFERROR(" & MidPointE & j & " / " & MidPointE &  CustomerLast & ", """")"

Quotes inside a string literal must be escaped - and that's done by doubling them up.

Comments

0

It's at the end here

& ", "")"

The two quotes between the comma and ) end the string and start a new one.

'first string
","

'second string
")"

Since you can't just throw two strings together like that, i would replace the two quotes with the CHR equivalent. Should look something like this:

Cells(j, Dist).Formula = "=IFERROR(" & MidPointE & j & " / " & MidPointE &  CustomerLast & ", "& CHR(034) & CHR(034) & ")"
'pretty sure 034 is the ascii code for "

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.