无法获取WorksheetFunction的MMult属性

时间:2011-12-16 20:48:59

标签: excel vba

大家,我遇到了一个奇怪的问题,请帮帮忙。

错误消息为:

  

运行时错误'1004'

     

无法获取MMult

WorksheetFunction属性

1,我使用程序名称“schedule”每天自动运行我的VBA程序,但是VBA程序每天都会失败,但是当我控制我的“计划程序”时再次自动运行它来尝试重现这个错误,我无法得到它,它运行顺利。

2,发生此错误时,Excel将显示 [结束] [调试] 窗口,我点击 [调试] 并按 [F5] ,它运行顺畅;如果MMult的参数不正确,它会再次显示错误。

3,我写了一个子转储我在mmult中使用的数据,发生错误同样没有错误。

所以,我可以假设mmult的两个参数是正确的,但为什么我每天都收到错误信息?

这个最难的事情是很难再现这个错误。

Public Function Regression(ByVal X As Variant, ByVal y As Variant)
    writelog ("Regression")
    writelog ("dump x")
    Call dumpRange(X, 2)
    writelog ("dump y")
    Call dumpRange(y, 1)

    Dim xtrans, temp, temp2, b
    xtrans = Application.WorksheetFunction.Transpose(X)
    temp = Application.WorksheetFunction.MMult(xtrans, X)     ' occour error on this line
    temp = Application.WorksheetFunction.MInverse(temp)
    temp2 = Application.WorksheetFunction.MMult(xtrans, y)
    b = Application.WorksheetFunction.MMult(temp, temp2)
    Regression = b
End Function

X是这样的范围

1  0.34343323
1  1.32323323
1  1.21111221
1  0.33444232
.  ...... 

Window 7 home preminum 64bit

办公室2010专业64位/ 32位sp1

3 个答案:

答案 0 :(得分:0)

试试这个。我认为这是你收到错误的地方

temp = Application.WorksheetFunction.MMult(xtrans,
Application.Transpose(X))

答案 1 :(得分:0)

我重新安装了我的操作系统和办公室,现在工作正常。

答案 2 :(得分:0)

我遇到了同样的问题。但事实证明我的是由尺寸问题引起的。

我做了:

Redim Y(obs)
Redim X(obs,3)
Xtransposed = Application.worksheetfunction.transpose(X)
MMult = Application.worksheetfunction.mmult(Xtransposed, Y) 

虽然我应该使用Redim Y(obs,1)来使代码正常工作。

所以:

Redim Y(obs,1)
Redim X(obs,3)
Xtransposed = Application.worksheetfunction.transpose(X)
MMult = Application.worksheetfunction.mmult(Xtransposed, Y)