7

When I add data to my workbook it is necassary to copy the formula from an earlier cell to the new cell(s).

I used the following formula to calculate my growth rate:

=WENN(ODER(K9="";L9="");"";WENNFEHLER((L9-K9)/K9;""))

Since this is very time consuming I want to improve it with a macro and have therefor written the following code:

Sub Growth()

Tabelle3.Range("O9").Formula = "=WENN(ODER(K9="";L9="");"";WENNFEHLER((L9-K9)/K9;""))"

Tabelle3.Range("O9:O14").FillDown


End Sub

However, when I want to run the code "runtime error '1004': application defined or object defined error" occours for this line:

Tabelle3.Range("O9").Formula = "=WENN(ODER(K9="";L9="");"";WENNFEHLER((L9-K9)/K9;""))"

Does anyone know why this error occurs and how I can solve it?

Vityata
  • 42,633
  • 8
  • 55
  • 100
HPM
  • 113
  • 1
  • 8

1 Answers1

13

You have two main errors in the code - not escapting the " characters and using .Formula instead of .FormulaLocal. The " characters should be written twice to show once in a string. See this code:

Public Sub TestMe()
    Debug.Print "test"""""
End Sub

It prints test"". The last " is for the end of the string. Concerning the formula, use .FormulaLocal if you want to use the German formulas and double the doublequotes:

Range("O9").FormulaLocal = "=WENN(ODER(K9="""";L9="""");"""";WENNFEHLER((L9-K9)/K9;""""))"

In general, avoid using .FormulaLocal and use .Formula, to make your VBA code compatible with a workbook from Italy or France from example. Something like this will work with any local settings in Excel:

Range("O9").Formula = "=IF(OR(K9="""",L9=""""),"""",IFERROR(((K9-K9)/K9),""""))"


What I usually do is the following:

  1. Write the formula, so it works in Excel;
  2. Select it manually;
  3. Run this:

Public Sub PrintMeUsefulFormula()

    Dim strFormula  As String
    Dim strParenth  As String

    strParenth = """"

    strFormula = Selection.Formula
    strFormula = Replace(strFormula, """", """""")

    strFormula = strParenth & strFormula & strParenth
    Debug.Print strFormula

End Sub
  1. It prints the formula as it should look like in the immediate window;
  2. Copy it;
  3. In the code above you may replace Selection.Formula with one of the following three (Consider as an example =IF(B1=C1,""Equal"",""Not Equal"")

    • Selection.FormulaLocal (gets the local formula =WENN() for Germany)
      =WENN(B1=C1;""Equal"";""Not equal"")
    • Selection.FormulaR1C1 (gets formula in R1C1 format)
      =IF(RC[1]=RC[2],""Equal"",""Not equal"")
    • Selection.FormulaR1C1Local (gets R1C1 format with local formulas) =WENN(ZS(1)=ZS(2);"Equal";"Not equal")
Vityata
  • 42,633
  • 8
  • 55
  • 100
  • 3
    10x @Vityata for your help :) – HPM Mar 19 '18 at 13:13
  • I also tried to extend my Range from Tabelle3.Range("O9:O14").FillDown to Tabelle3.Range("O9:O").FillDown. It occurs "runtime error 1004 method 'range' of object '_workbook' failed". Do you probably know whats wrong with this one too? :) @Vityata – HPM Mar 19 '18 at 15:40
  • @HPM - you need to have a normal range. E.g. `Range("O:O")` or `Range(O1:O5)`. `Range(O1:O)` is illegal. This is something good to read - https://www.rondebruin.nl/win/s9/win005.htm – Vityata Mar 19 '18 at 15:41