简体   繁体   中英

#NAME? Error in Excel VBA

So I have something like this:

        A        B        C      ...
1       11       12       13
2       10       20       15
3       1        -8       -2
...

So A3 , B3 , and C3 is generated by subtracting A1 to A2 and so on.

If you look at the top of the sheet there is a long formula bar that shows the formula of a cell when you click on one. If you are filling in the sheet manually, you can type in that bar something like = A1 - A2 and it will fill A3 for you.

In my case, I am using .Formula = "IFERROR(A1 - A2, ""N/A""") in my code.

The problem I am having is that when the sheet is generated, instead of the desired output shown above, it is displaying something like this

       A        B        C      ...
1      11       12       13
2      10       20       15
3      #NAME?   #NAME?   #NAME?
...

If I click on the cell, the formula bar actually shows that it is apply the correct formula, and if I hit Enter after clicking on the formula bar, the correct numbers show up. So it's like I'm manually entering the formula.

This is my code. ConvertToLetter() takes in an integer and convert to column character.

Public Function ProcessExcelRpt(dataArray(,) As Object) As Integer
   Dim ws As Worksheet
   Dim r As Range

   Try
       For i As Integer = 1 To xlWorkBook.Sheets.Count
           ws = xlWorkBook.Sheets(i)

           r = ws.Range("A8")
           ws.Range("A8").Resize(dataArray.GetUpperBound(0) + 1, dataArray.GetUpperBound(1) + 1).Value2 = dataArray
           ws.Range("A2").Value2 = ws.Range("A2").Value2.ToString() & FormatDate(ReportDate, "MMMM dd, yyyy")

           FormatColumns(ws, 8, dataArray.GetUpperBound(0) + 8)
           excel.CalculateFull()

           xlWorkBook.SaveAs(saveAs)
           Exit For
       Next
   Catch ex As Exception    
        Return -1
   End Try  

   Return 0
End Function

Public Sub FormatColumns(ws As Worksheet, ByVal firstRow As Integer, ByVal lastRow As Integer)
    Dim rng As Range

    Try
        Dim colCnt, rowCnt, i As Integer
        i = 0
        For rowCnt = firstRow To lastRow
           Dim row1, row2 As Integer

           row1 = rowCnt - 2 ' go back 2 rows 
           row2 = rowCnt - 1 ' go back 1 row

           ' Apply formula to each cell in each row
           For colCnt = 1 To 3
               rng = ws.Range(ConvertToLetter(colCnt) & rowCnt) ' A1 for ex  
               rng.Formula = "=IFERROR(" & ConvertToLetter(colCnt) & row1 & "-" & ConvertToLetter(colCnt) & row2 & ", ""N/A"")"
           Next

       Next      
    Catch ex As Exception

    End Try
End Sub

If you run this as a Sub it is working for me:

Sub foo(firstRow, lastRow)
Dim rng As Range
For rowCnt = firstRow To lastRow
   Dim row1, row2 As Integer

   row1 = rowCnt - 2 ' go back 2 rows
   row2 = rowCnt - 1 ' go back 1 row

   ' Apply formula to each cell in each row
   For colCnt = 1 To 3
      Set rng = Cells(rowCnt, colCnt) ' A1 for ex
      rng.Formula = "=IFERROR(" & Cells(row1, colCnt).Address & "-" & Cells(row2, colCnt).Address & ", ""N/A"")"
   Next

Next
End Sub

If you are trying to execute this from a Function call as a UDF, it will not work because of explicit limitations on what UDFs can manipulate on the worksheet, specifically that you cannot:

Change another cell's value.

https://support.microsoft.com/en-us/kb/170787

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM