堆栈外空间vba excel

堆栈外空间vba excel,vba,excel,Vba,Excel,我无法解决这个错误。。我曾尝试在线搜索,但似乎找不到解决此问题所需的解决方案。它显示“堆栈外”错误(运行时错误“28”)。有人能帮忙吗 Option Explicit Dim ws As Sheets Dim i As Integer, j As Integer Public Function test1(i, j) Set ws = Sheets(Array("Sheet1", "Sheet2")) With Application.WorksheetFunction test1

我无法解决这个错误。。我曾尝试在线搜索,但似乎找不到解决此问题所需的解决方案。它显示“堆栈外”错误(运行时错误“28”)。有人能帮忙吗

Option Explicit
Dim ws As Sheets
Dim i As Integer, j As Integer


Public Function test1(i, j)

Set ws = Sheets(Array("Sheet1", "Sheet2"))
 With Application.WorksheetFunction

  test1(i, j) = .Index(ws(2).Range("B2:D5"), .Match(ws(1).Range("A" & i), ws(2).Range("A2:A5"), 0), .Match(ws(1).Range("B" & j), ws(2).Range("B1:D1"), 0))

 End With

End Function

Sub Xecute()
Set ws = Sheets(Array("Sheet1", "Sheet2"))
 For i = 5 To 13 Step 4
   For j = 5 To 16

    test1(i, j) = ws(1).Range("C" & j).Value

   Next j

 Next i

 End Sub
我的更正代码:

Option Explicit
Dim ws As Sheets
Dim i As Integer, j As Integer, p As Integer, q As Integer


Public Function test1(i, j)

Set ws = Sheets(Array("Sheet1", "Sheet2"))
With Application.WorksheetFunction

 test1 = .Index(ws(2).Range("B2:D5"), .Match(ws(1).Range("A" & i), ws(2).Range("A2:A5"), 0), .Match(ws(1).Range("B" & j), ws(2).Range("B1:D1"), 0))

End With

End Function

Sub Xecute()
Set ws = Sheets(Array("Sheet1", "Sheet2"))
 For i = 5 To 13 Step 4
  For j = 5 To 16

   ws(1).Range("C" & j).Value = test1(i, j)


  Next j
 Next i

End Sub

“堆栈外”错误是由于您为变量分配的数字超出了它所能容纳的范围而导致的。例如,
i和J被标为整数
,但是它所持有的值可能大于32000或更大。因此,请尝试将变量声明为
double或variant

为什么要为函数赋值?i、 e.
test1(i,j)=…
?嘿,是的,我刚刚发现了这一点,正如我说的,我是VBA新手。。我纠正了那个错误。它现在正在工作:)但是我现在有一个新问题,我想我的子Xecute()中的循环有问题。作为范围(“C”&j)。值由对应于i=13的值填充。我想做的是,(C4:C8)应该对应于I=5,(C8:C12)对应于I=9,依此类推。。你能帮忙吗?你能把答案贴出来吗?