Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2005
    Posts
    25

    Unanswered: Formula does not work when used in VBA code

    OK...so i'm a little perplexed at this point.

    Trying to replicate a worksheet using VBA code to build and I have run into a problem. The following line causes an "application-defined or object-defined error":

    Range("AA3").Formula = "=IF(X3 ="",IF(W3="",N3+V3,N3+W3), N3+X3)"

    What is really bothering me is the fact that the formula is the exact same formula in the cell of the sheet that I am trying to replicate. If I copy and pasted this formula into a cell it would work as intented, but in the line above, it generates an error. Is there something that i'm missing here?

  2. #2
    Join Date
    Sep 2008
    Location
    London, UK
    Posts
    511
    Hi Mike,

    In VBA, the " character denotes a string so, to include a " in a string, you have to do it like this:
    Code:
    Range("AA3").Formula = "=IF(X3 ="""",IF(W3="""",N3+V3,N3+W3), N3+X3)"
    (or you can use Chr 34 to represent them)
    Code:
    Range("A3").Formula = "=IF(X3 =" & Chr$(34) & Chr$(34) & ",IF(W3=" & Chr$(34) & Chr$(34) & ",N3+V3,N3+W3), N3+X3)"

  3. #3
    Join Date
    Mar 2005
    Posts
    25
    That did it! Thanks!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •