• Access VBA formula in Excel (A2003 SP 2)

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Access VBA formula in Excel (A2003 SP 2)

    Author
    Topic
    #452454

    I know I have this asked and answered before, but I can’t for the life of me find it.
    I am using the following VBA code from an Access module to put a formula in a specific cell
    strSQL = “P2:P” & r
    .Range(strSQL).FormulaR1C1 = “=IF(SUM(J2-H2-M2)<U2,(J2-H2-M2),U2)"

    When I open the spreadsheet instead of the above, I have "=IF(SUM('J2'-'H2'-'M2')<'U2',('J2'-'H2'-'M2'),'U2')". For some reason I am getting apostrophe's (') after each cell. What am I doing wrong?

    Thanks in advance for your guidance.

    Viewing 0 reply threads
    Author
    Replies
    • #1117229

      FormulaR1C1 expects a formula using R1C1 style cell refererences, not A1 style cell references, so your code is inconsistent. Try

      .Range(strSQL).Formula = “=IF(SUM(J2-H2-M2)<U2,(J2-H2-M2),U2)"

      By the way, it can be simplified to

      .Range(strSQL).Formula = "=MIN(J2-H2-M2,U2)"

      Note: strSQL is a confusing name for a variable that holds a range reference.

      • #1117290

        Hans,

        You are right about the strSQL, but I needed a variable and that one was already diminsioned, so I was lazy and used it. I need to be more methodical on how I use things. I’ll give it a try, and I have no doubt it will work. THANKS!

        As a side note, do you know if there was a reason that it automatically placed the apostrophe’s in the cell? Or is it just one of those anomalies?

        THANKS!

        Ken

        • #1117292

          The cause of the apostrophes was that the formula was not valid as an R1C1 style formula, so Excel tried to interpret the cell references as names.

          • #1117295

            THANK YOU. It’s hard to outsmart these things when they are so logical all the time.

    Viewing 0 reply threads
    Reply To: Access VBA formula in Excel (A2003 SP 2)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: