Home » excel » excel – Cells(x,y).formula generates a #NAME error when referring to another sheet

excel – Cells(x,y).formula generates a #NAME error when referring to another sheet

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying fill several cells in a sheet with a formula (Something along the lines of “=ALS($AFB$14-$AFB$13 Input!$T$2,Input!$U$2-$AFB$14,0)” (If formula, Excel is in Dutch), with Input being another sheet in the same file. It fills in the correct formula, but it gives a #Name-error. If I manually select the cell and enter the exact formula VBA generated, excel recognizes it correctly. I suppose i do something wrong in referring to other sheets, but I’m not sure what

'Excel is in Dutch, this would be the English formula              
            'formula =  "=IF(" & Cells(row - 1, column).Address & "-" & Cells(row - 2, column).Address & "<" & Inp.Cells(sku + 1, 20).Address(External:=True) & ";" & Inp.Cells(sku + 1, 21).Address(External:=True) & "-" & Cells(row - 1, column).Address & ";0)"

            formula = "=ALS(" & Cells(row - 1, column).Address & "-" & Cells(row - 2, column).Address & "<" & Inp.Cells(sku + 1, 20).Address(External:=True) & "," & Inp.Cells(sku + 1, 21).Address(External:=True) & "-" & Cells(row - 1, column).Address & ",0)"


      MRP.Cells(16 * sku - 1, 827 + i).formula = formula
How to&Answers:

Whenever you are using .Formula, it does not matter that Excel is in Dutch. It uses the English formula.

In order to use the Dutch (or the local language), use .FormulaLocal. To see a good example of the formulas in Excel and how they are referred to by VBA, run the following on a new Workbook:

Public Sub SelectAndTestMe()
    With Range("A1")
        .Formula = "=IF(A2=1,""OK"",""Not OK"")"
        Debug.Print .Formula
        Debug.Print .FormulaLocal
        Debug.Print .FormulaR1C1
    End With
End Sub

This is what you will get (if you are using German Excel) in the immediate window:

=IF(A2=1,"OK","Not OK")
=WENN(A2=1;"OK";"Not OK")
=IF(R[1]C=1,"OK","Not OK")