Excel - Inserting formula with VBA

后端 未结 3 1138
遇见更好的自我
遇见更好的自我 2020-12-11 10:16

Hello Stackoverflow friends,

I am struggling for 1 hour with a formula I would like to insert via VBA:

Formula = \"=IFERROR(VLOOKUP(Q\" & j &         


        
3条回答
  •  借酒劲吻你
    2020-12-11 10:54

    When programming in any lanugage also in VBA - better not tied up user to specific regional settings or specific excel version. So instead of this:

    Formula = "=IFERROR(VLOOKUP(Q" & j & ";Table1[#All];2;FALSE);"""")"
    ThisWorkbook.Worksheets("Sheet1").Cells(j, "AE").FormulaArray = Formula
    

    Better use this approach, when you determine exact user environment:

    s = Application.International(xlListSeparator)
    Formula = "=IFERROR(VLOOKUP(Q" & j & s +"Table1[#All]" + s + "2" + s + "FALSE)" + s + """"")"
    ThisWorkbook.Worksheets("Sheet1").Cells(j, "AE").FormulaArray = Formula
    

    p.s. I didn't checked the formula for the brackets etc. but just indicating the correct usage of list separator, and how to insert formulas with VBA code within cells in correct way.

    As well, as previous post says - excel probably change the formula automatically when you open it. However excel do not change VBA code automatically, so be aware and pay attention to proper code in VBA.

提交回复
热议问题