Excel VBA:#VALUE!某些选定范围的错误

Excel VBA: #VALUE! Error with Certain Selected Ranges

你好,我最近一直在学习 Excel VBA 和宏,但是我 运行 在进行练习时遇到了问题.. .我有一个开始产生#VALUE 的函数!某些 selected 范围出错,我对如何修复它有点困惑。该函数根据自动填充的单元格进行不同的计算。函数如下:

Function CalculateStuff(myRange As Range) As Double
   Application.Volatile

   Dim numRows As Long
   numRows = myRange.Rows.Count

   Dim whatColumn As Long
   whatColumn = Application.Caller.Column - myRange.Column

   Dim i As Long
   Dim thing As Double
   Dim mainThing As Double

   Select Case whatColumn
      Case 0
         CalculateStuff = WorksheetFunction.Sum(myRange.Columns(1))
      Case 1
         CalculateStuff = WorksheetFunction.SumProduct(myRange.Columns(1), myRange.Columns(2))
      Case 2
         CalculateStuff = WorksheetFunction.SumProduct(myRange.Columns(1), myRange.Columns(3)) / WorksheetFunction.Sum(myRange.Columns(1))
      Case 3
         CalculateStuff = WorksheetFunction.SumSq(myRange.Columns(4))
      Case 4
         For i = 1 To numRows
            thing = myRange(i, 1) * WorksheetFunction.SumSq(myRange(i, 3), myRange(i, 5))
            mainThing = mainThing + thing
         Next i
         CalculateStuff = mainThing
    End Select

End Function

然后我在子例程中使用它来填充活动单元格的公式并自动填充到活动单元格的右侧。子程序如下:

Sub RunCalculateStuff()
   Dim initialRange As Range
   Set initialRange = Application.InputBox("Please select the cells in the first column you would like to use...", Type:=8)

   Dim finalRange As Range
   Dim i As Long
   i = 0
   For Each initialCell In initialRange
      If i = 0 Then
         Set finalRange = ActiveSheet.Range(initialCell, initialCell.Offset(0, 4))
         i = i + 1
      Else
         Set finalRange = Application.Union(finalRange, ActiveSheet.Range(initialCell, initialCell.Offset(0, 4)))
         i = i + 1
      End If
   Next

   ActiveCell.Formula = "=CalculateStuff(" + finalRange.Address + ")"

   ActiveCell.AutoFill Destination:=ActiveSheet.Range(ActiveCell, ActiveCell.Offset(0, 4))
End Sub

该子例程的工作原理是让用户 select 他们想要使用的第一列中的单元格,然后循环遍历这些单元格并将单元格抓取到距离 (0, 4) 的偏移量,并且将该范围添加到总体范围。然后将这个范围提供给这个函数,它就开始了。

这里是#VALUE!出现错误...只有当我 select 的单元格不是连续的时才会发生...我的意思是,如果我 select 范围 AA1:AA4 带有初始获取框,它工作得很好。如果我 select 范围 AA1、AA2、AA3、AA4 单独,它工作得很好。但是,如果我 select 范围 AA1、AA3,我会得到 #VALUE!错误。我觉得它与跳过行有关,但我真的不明白为什么,因为我正在将 Union 放入自己的范围内。另外,当我只是尝试在第一次计算中对范围的第一列求和时它失败了,然后它在其余部分也失败了。下面的屏幕截图说明了我的意思。

工作范围:

破碎范围:

提前感谢您的帮助!非常感谢。

这似乎对我有用。如果您运气不好或需要灵感,请发帖。请注意,我没有进行任何错误检查以查看您输入的所有元素是否都是范围。

Function CalculateStuff2(ParamArray Rngs()) As Double

    Dim i As Integer
    Dim col As Long
    Dim tmpRng As Range
    Dim tmpDbl As Double
    Dim divisor As Double
    Dim IsCase2 As Boolean
    Dim numRows As Long, r As Long

    For i = LBound(Rngs()) To UBound(Rngs())
        Set tmpRng = Rngs(i)
        col = Application.Caller.Column - tmpRng.Column
        numRows = tmpRng.Rows.Count

        Select Case col
            Case 0
                tmpDbl = tmpDbl + WorksheetFunction.Sum(tmpRng.Columns(1))
            Case 1
                tmpDbl = tmpDbl + WorksheetFunction.SumProduct(tmpRng.Columns(1), tmpRng.Columns(2))
            Case 2
                IsCase2 = True
                tmpDbl = tmpDbl + WorksheetFunction.SumProduct(tmpRng.Columns(1), tmpRng.Columns(3))
                divisor = divisor + WorksheetFunction.Sum(tmpRng.Columns(1))
            Case 3
                tmpDbl = tmpDbl + WorksheetFunction.SumSq(tmpRng.Columns(4))
            Case 4
                For r = 1 To numRows
                    tmpDbl = tmpDbl + tmpRng(r, 1) * WorksheetFunction.SumSq(tmpRng(r, 3), tmpRng(r, 5))
                Next r
        End Select
    Next i

    If IsCase2 Then
        CalculateStuff2 = tmpDbl / divisor
    Else
        CalculateStuff2 = tmpDbl
    End If
End Function