Write a formula in an Excel Cell using VBA

后端 未结 5 628
Happy的楠姐
Happy的楠姐 2020-12-11 01:01

I\'m trying to use VBA to write a formula into a cell in Excel. My problem is that when I use a semicolon (;) in my formula, I get an error:

相关标签:
5条回答
  • 2020-12-11 01:08

    You can try using FormulaLocal property instead of Formula. Then the semicolon should work.

    0 讨论(0)
  • 2020-12-11 01:08

    The correct character (comma or colon) depends on the purpose.

    Comma (,) will sum only the two cells in question.

    Colon (:) will sum all the cells within the range with corners defined by those two cells.

    0 讨论(0)
  • 2020-12-11 01:19

    The correct character to use in this case is a full colon (:), not a semicolon (;).

    0 讨论(0)
  • 2020-12-11 01:20

    Treb, Matthieu's problem was caused by using Excel in a non-English language. In many language versions ";" is the correct separator. Even functions are translated (SUM can be SOMMA, SUMME or whatever depending on what language you work in). Excel will generally understand these differences and if a French-created workbook is opened by a Brazilian they will normally not have any problem. But VBA speaks only US English so for those of us working in one (or more) foreign langauges, this can be a headache. You and CharlesB both gave answers that would have been OK for a US user but Mikko understod the REAL problem and gave the correct answer (which was also the correct one for me too - I'm a Brit working in Italy for a German-speaking company).

    0 讨论(0)
  • 2020-12-11 01:23

    I don't know why, but if you use

    (...)Formula = "=SUM(D2,E2)"
    

    (',' instead of ';'), it works.

    If you step through your sub in the VB script editor (F8), you can add Range("F2").Formula to the watch window and see what the formular looks like from a VB point of view. It seems that the formular shown in Excel itself is sometimes different from the formular that VB sees...

    0 讨论(0)
提交回复
热议问题