首页 > 解决方案 > 查找 VBA 数组的长度

问题描述

我有一个自定义 Excel 函数,它应该采用一个范围并计算值的数量,其中一些单元格可能包含多个/- 分隔的值,每个值都必须计算在内。这是功能:

Option Explicit

Public Function COUNT_RACKS(rack_range As Range) As Integer
    Dim total As Integer
    Dim cell As Range
    Dim split_str As Variant
    Dim len_str As Integer
    total = 0
    For Each cell In rack_range.Cells
        If Len(cell.Value) > 0 Then
            If InStr(cell.Value, "/") <> 0 Then
                split_str = Split(cell.Value, "/")
                len_str = Len(split_str)
                total = total + len_str
            Else
                total = total + 1
            End If
        End If
    Next cell
    COUNT_RACKS = total
End Function

问题是,它会抛出#VALUE!错误。在调试器中单步执行该函数时,该行似乎存在问题len_str = Len(split_str)。我这样说是因为每当我在调试器中到达这一行时,它会立即停止调试,并且本质上是一个F5. 显然我的代码中有一个或多个错误,但是如果我的调试器不愿意向我显示错误,我该如何调试它?

代码有什么问题?

标签: excelvba

解决方案


推荐阅读