【问题标题】:VBA Code: Value error with minimal function code (array input/output)VBA 代码:具有最小功能代码的值错误(数组输入/输出)
【发布时间】:2021-05-13 11:41:10
【问题描述】:

我仍然是 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 会出错吗?非常感谢您的帮助...非常感谢!

【问题讨论】:

  • 你是如何加载Old数组的?是二维数组吗?哪一行会引发错误?是R = UBound(Old, 1)吗? Old 是数组还是范围? Variant 声明接受这两种情况。如果数组Old.Value2 没有任何意义。如果它是一个数组,那么R = UBound(Old, 1) 是不合适的......请编辑您的问题并向我们展示调用该函数的 Sub。
  • Old 是一个范围吗?你使用Old.Value2 所以我猜是这样。您传递给函数的单元格中有什么?
  • 旧已被定义为“变体”(见上一行)。 .Value2 仍然有效,即使它不是一个范围。我为传递给函数的单元格尝试了不同的单元格值(nums,fromulas,没关系,工作/不工作切换是注释的代码块)。

标签: arrays excel vba


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 2012-05-25
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多