简体   繁体   English

#名称? Excel VBA中的错误

[英]#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. 所以A3B3 ,和C3减去产生的A1A2等。

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. 如果您是手动填写工作表,则可以在该栏中输入= A1 - A2 ,它将为您填充A3

In my case, I am using .Formula = "IFERROR(A1 - A2, ""N/A""") in my code. 就我而言,我在代码中使用.Formula = "IFERROR(A1 - A2, ""N/A""")

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. 如果我单击该单元格,则公式栏实际上表明它正在应用正确的公式,如果在单击公式栏后单击Enter,则会显示正确的数字。 So it's like I'm manually entering the formula. 就像我手动输入公式一样。

This is my code. 这是我的代码。 ConvertToLetter() takes in an integer and convert to column character. ConvertToLetter()接受一个整数并转换为列字符。

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运行,则对我有用:

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: 如果您尝试通过Function调用作为UDF执行此操作,则该操作将不起作用,因为对工作表上的UDF可以进行哪些操作有明确的限制,特别是您不能:

Change another cell's value. 更改另一个单元格的值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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