首页 > 解决方案 > VBA 代码:具有最小功能代码的值错误(数组输入/输出)

问题描述

我仍然是 VBA 编码的新手,并且有一些基本代码来测试功能性然后对其进行扩展。

某个代码块似乎完全破坏了功能(当我注释该块时,代码工作得很好),尽管在注释/未注释的代码块中没有什么“发生”。

Function Linearize(Old As Variant) As Variant

'    Dim R As Integer
'    Dim C As Integer
'    Dim L As Integer
'
'    R = UBound(Old, 1)
'    C = UBound(Old, 2)
'    L = R * C

    Dim NewA As Variant

    NewA = Old.Value2

    Linearize = NewA
    
End Function

当我取消注释代码块时,出现 VALUE 错误...“公式中使用的值的数据类型错误。”

我只是没有看到 WTF 可能出错了?非常感谢您的帮助...非常感谢!

标签: arraysexcelvba

解决方案


通过删除错误调用来解决问题的一种简单方法:

Function linearize(old As Variant) As Variant

    Dim R
    Dim C
    Dim L As Integer

    R = UBound(old, 1)
    C = UBound(old, 2)
    L = R * C
    
    Debug.Print L


End Function

在另一个子中,您可以调用该函数

Sub test2()
Dim myarray(1 To 10, 50 To 100)

linearize (myarray)

End Sub

推荐阅读