Consulting

Results 1 to 9 of 9

Thread: Solved: Formula property compile error

  1. #1
    VBAX Regular
    Joined
    Feb 2009
    Posts
    34
    Location

    Solved: Formula property compile error

    I know this is a noob question but I am stuck and cant find the answer. Can any ine tell me why i get a compile error in vba using this formula property code.
    Selection.Formula = _
    "=IF(G2="CONSTRUCTION"," '.600")"

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    Are you sure you have the formula constructed correctly? Excel requires the following IF (Logical Test,[Value if True],[Value if False]). As you have written it, the formula seems to fail in the value if true/false components.

    Did you mean "=IF(G2="Construction","",".600")"
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Hi v,

    Not so noob. VBA considers "xxx" to be a string so it requires any " inside the string to be dbl quted as well as well.

    This will work:

    Range("A1") = "=IF(G2=""CONSTRUCTION"","""","".600"")"

    Looks confusing but notice that the beginning quote:

    "=IF( <snip> and the ending quote <snip> .600)" enclose the complete string.

    All the ones in the formula itself are enclosed in their own quotes:

    "CONSTRUCTION" becomes ""CONSTRUCTION""

    "" becomes """"

    and ".600" becomes "".600""

    When the formula is read and placed in the cell VBA drops all the extra "" and so the result is what you wanted:

    =IF(G2="CONSTRUCTION","",".600")

    BTW single ' ' are special too!
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  4. #4
    VBAX Regular
    Joined
    Oct 2009
    Location
    Fremont, CA
    Posts
    72
    Location

    An Alternative

    Thanks, dr, for the detailed explanation on how to deal with strings inside strings. I am sure double-double quote syntax has been bugging generations of both noob as well as seasoned programmers.

    I have run across a different approach I thought I should throw into the discussion. It doesn't neccessarily make for more concise code, but reminds you at what you need to be aware of.

    I prefer to define a string for the double quote, e.g.

    [vba] Dim d As String 'double quote
    d = Chr$(34)
    [/vba]

    Now the formula in this conversation can be written this way:

    [vba]"=If(G2=" + d + "Construction" + d + "," + d + "" + d + "," + d + ".600" + d + ")"[/vba]
    Hope this helped,
    Rolf Jaeger
    SoarentComputing
    Software Central

  5. #5
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by RolfJ
    Thanks, dr, for the detailed explanation on how to deal with strings inside strings. I am sure double-double quote syntax has been bugging generations of both noob as well as seasoned programmers.

    I have run across a different approach I thought I should throw into the discussion. It doesn't neccessarily make for more concise code, but reminds you at what you need to be aware of.

    I prefer to define a string for the double quote, e.g.

    [vba] Dim d As String 'double quote
    d = Chr$(34)
    [/vba]
    Now the formula in this conversation can be written this way:

    [vba]"=If(G2=" + d + "Construction" + d + "," + d + "" + d + "," + d + ".600" + d + ")"[/vba]
    you should use the ampersand (&) instead of the plus sign (+) when building compound strings. The + operator should be used for numerical "additions". I have found cases where VBA got confused when I used "+" instead of "&" in string/text operations.
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

  6. #6
    VBAX Regular
    Joined
    Oct 2009
    Location
    Fremont, CA
    Posts
    72
    Location

    You are right of course, but

    Yes, yes, you are right, and it's probably not good practice to post a reply using + for string concatenation without qualifying why it would be OK in this case. Me bad!

    In general I too use the & character, however in this particular case using it made the whole formula look even more unwieldy.

    Now, as you probably know, the problem you are referring to with using the + character has to do with the automatic typecasting VBA employs. IF 'String' is the string representation of a number, but only then, the construct 'String + Number' automatically typecasts the string to a number and returns a number which then gets again automatically typecasted back to a string if that's required in the context. HOWEVER if 'String' is NOT the string representation of a number the construct 'String + Number' returns an error. THEREFORE as long as I only concatenate strings with the + character it IS INDEED equivalent to the & character.
    Hope this helped,
    Rolf Jaeger
    SoarentComputing
    Software Central

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    If you have a coomplex workshhet formula, you can easily get the code for it. Select the cell, start the macro recorder, press F2 and Enter. Stop recording. You should then have
    [vba]
    Sub Macro1()
    '
    ' Macro1 Macro
    '
    '
    ActiveCell.FormulaR1C1 = "=IF(R[-12]C[3]=""CONSTRUCTION"","" '.600"")"
    Range("D15").Select
    End Sub

    [/vba]
    which you can then tidy up.

    You could also consider a simple function

    [vba]
    Sub Macro1()
    ActiveCell.FormulaR1C1 = "=IF(R[-12]C[3]=" & DQ("Construction") & "," & DQ(".600") & ")"
    End Sub

    Function DQ(Data As String)
    DQ = """" & Data & """"
    End Function
    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    I can not think of the number of times the solution to a problem was to turn on the macro recorder, do it manually, turn off the recorder and then see what VBA was generated.
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

  9. #9
    VBAX Regular
    Joined
    Feb 2009
    Posts
    34
    Location
    Thanks to all that posted. It really helped. Here is what my code looks l ike for all that is interested. I am going to work on cleaning it up today as per this post but this code work fine also.
    [vba]
    Sub BudElCodeRes()
    With Application
    .ScreenUpdating = False
    Range("W1").Select
    ActiveCell = ("WP_ID")
    Do While IsEmpty(ActiveCell.Offset(0, -2)) = False
    ActiveCell.Offset(1, 0).Select
    Selection.Formula = "=IF(G" & ActiveCell.Row & "=""CONSTRUCTION"","".600"",IF(G" & ActiveCell.Row _
    & "=""MISC"","".00"",IF(G" & ActiveCell.Row & "=""ROW"","".300"",IF(G" & ActiveCell.Row & "=""PREDESIGN"", _
    "".100"",IF(G" & ActiveCell.Row & "=""DETAIL DESIGN"","".206"",IF(G" & ActiveCell.Row & "=""CONSERV"","".600""))))))"
    ActiveCell.Offset(0, -1).Select
    Selection.Formula = _
    "=CONCATENATE(U" & ActiveCell.Row & ",W" & ActiveCell.Row & ")"
    ActiveCell.Offset(0, 1).Select
    Loop
    .ScreenUpdating = True
    End With
    End Sub
    [/vba]

Posting Permissions

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