运行时错误1004 MDeterm属性

时间:2018-05-03 02:19:59

标签: excel vba

我写了这个函数来计算多边形的面积。由于某些原因,当我在DataRange(4) = 0中的最后一个值时,我在Excel工作表中得到 #VALUE!

在代码方面,部分X3 = WorksheetFunction.MDeterm(array3)正在获取:

  

运行时错误1004:无法立即获取MDeterm属性   窗户。

错误截图

enter image description here

Public Function polygon(DataRange As Variant)
    Dim Area As Variant
    Dim X1 As Double
    Dim X2 As Double
    Dim X3 As Double
    Dim X4 As Double

    Dim array1(1 To 2, 1 To 2) As Variant
    Dim array2(1 To 2, 1 To 2) As Variant
    Dim array3(1 To 2, 1 To 2) As Variant
    Dim array4(1 To 2, 1 To 2) As Variant

    array1(1, 1) = 0
    array1(1, 2) = DataRange(1)
    array1(2, 1) = DataRange(2)
    array1(2, 2) = 0

    array2(1, 1) = DataRange(2)
    array2(1, 2) = 0
    array2(2, 1) = 0
    array2(2, 2) = -(DataRange(3))

    array3(1, 1) = 0
    array3(1, 2) = -(DataRange(3))
    array3(2, 1) = -(DataRange(4))
    array3(2, 2) = 0

    array4(1, 1) = -(DataRange(4))
    array4(1, 2) = 0
    array4(2, 1) = 0
    array4(2, 2) = DataRange(1)

    X1 = WorksheetFunction.MDeterm(array1)
    X2 = WorksheetFunction.MDeterm(array2)
    X3 = WorksheetFunction.MDeterm(array3)
    X4 = WorksheetFunction.MDeterm(array4)



    Area = Abs(0.5 * (X1 + X2 + X3 + X4))
    polygon = Area

End Function

1 个答案:

答案 0 :(得分:0)

向该函数提供2x2零的数组没有问题:

Sub Tester()

    Dim arr(1 To 2, 1 To 2)
    arr(1, 1) = 0
    arr(1, 2) = 0
    arr(2, 1) = 0
    arr(2, 2) = 0

    Debug.Print WorksheetFunction.MDeterm(arr) '>> 0

End Sub

你的案子正在发生其他事情。将数组声明为数字类型而不是变体可能有助于追踪问题。