VBA 中变体数据类型的算术运算

Arithmetic Operation for Variant Data Type in VBA


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。我已经尝试了一些更改,比如将变量的所有数据类型转换为变体,但它仍然给出相同的错误。谢谢

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

total = total + temp(1)