为什么即使类型似乎匹配,我也会遇到类型不匹配错误?

时间:2017-05-29 22:46:53

标签: arrays vba type-mismatch

所以我编写了一个程序,简单地说,接收了一堆信息,然后移动一些信息,然后尝试将其中一个数组传递给另一个执行某些数学运算的函数。

但是,当我尝试调用该函数(AllODES())时,它表示存在类型不匹配。它说它期待一个阵列。

以下是代码:

Public Function DetermineVolume(x As Double, xmax As Double, Flows As Range, h As Double, error As Double, temp As Double, diameter As Double, pressure As Double) As Double()

Dim i, j, m As Integer
Dim k(9, 9), Y5(9), Y4(9), Y4Old(9), ka(3), Kc(3), MW(7), rho(7) As Double
Dim delta0(9), delta1(9), delRatio(9) As Double, Rmin, FT, vol_F As Double

For i = 1 To 7                                                      'Moving the input data so it can acutally be used
    Y4(i) = Flows(i) 'mol/s
Next i

Y4(8) = pressure

                                                                            'k(Order #, equation #)
    For j = 1 To 6                                                          'First to 6th order


        For i = 1 To 7
            rho(0) = rho(0) + rho(i) * Y4(i)       'Calculate average density of mixture
            FT = FT + Y4(i)
            vol_F = vol_F + Y4(i) * MW(i) / rho(i)      'Calculating the total volumetric flowrate (m^3/s)
        Next i

        rho(0) = rho(0) / FT

        For i = 1 To 8                         'Calculating all of the k(1) values for eq 1 to 8
            k(j, i) = AllODES(x, Y4, i, j, k, h, temp, diameter, vol_F, rho(0)) 
        Next i 'CODE BUGS OUT HERE AND SAYS Y4 is a type mismatch
    Next j

DetermineVolume = Y4

End Function

Public Function AllODES(ByVal x As Double, Y() As Double, EqNumber As Integer, order As Integer, k() As Double, h As Double, _
 temp As Double, D As Double, vol_F As Double, rho As Double) As Double

'Some math operations are done in here

 AllODES=x

End Function

所以我的问题如下:

导致此类型不匹配错误的原因(因为我似乎是为数组传递数组),以及如何修复此错误。

提前致谢!

1 个答案:

答案 0 :(得分:2)

您收到错误是因为您将Variant数组传递给期望Double数组的函数。

你可能打算按如下方式对它们进行标注:

Dim i As Integer, j As Integer, m As Integer
Dim k(9, 9) As Double, Y5(9) As Double, Y4(9) As Double, Y4Old(9) As Double, ka(3) As Double, Kc(3) As Double, MW(7) As Double, rho(7) As Double
Dim delta0(9) As Double, delta1(9) As Double, delRatio(9) As Double, Rmin As Double, FT As Double, vol_F As Double