【问题标题】:Arithmetic Operation for Variant Data Type in VBAVBA中变体数据类型的算术运算
【发布时间】:2020-11-17 06:24:31
【问题描述】:

Public Sub MMULT()

Dim temp As Variant
Dim total As Double
Dim a, b As Range
Dim i As Integer

total = 0.45

For i = 1 To 9
Set a = Range(Cells(i + 15, 3), Cells(i + 15, 11))
Set b = Range(Cells(16, 14), Cells(24, 14))
temp = Application.WorksheetFunction.MMULT(a, b)
total = total + temp
Next i

Range("M9").Select
Range("M9").Value = total

End Sub

您好,有人可以帮我解决这个错误吗?我在第 14 行收到一个消息框“类型不匹配”,其中包含代码:total = total + temp。我已经尝试了一些更改,例如将变量的所有数据类型转换为变体,但它仍然给出相同的错误。谢谢

【问题讨论】:

标签: excel vba variant


【解决方案1】:

MMULT 返回一个数组(即使只返回一个值)——你不能添加一个 Double 和一个数组。

total = total + temp(1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-29
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 2011-10-24
    相关资源
    最近更新 更多