简体   繁体   English

超出堆栈空间的VBA Excel

[英]Out of stack space vba excel

and I am unable to resolve this error.. I have tried searching online but can't seem to find the solution required for this problem. 并且我无法解决此错误。。我尝试在线搜索,但似乎找不到该问题所需的解决方案。 It shows an "Out of stack" error(Run time error '28'). 它显示“堆栈外”错误(运行时错误“ 28”)。 Can someone please help? 有人可以帮忙吗?

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

My corrected code : 我更正的代码:

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

"Out of stack" error are caused when you assign the variable with large number more than it can hold. 当分配的变量数量超过其可容纳的数量时,将导致“堆栈外”错误。 for example, i and J are decalred as integer , however the value it hold might be greated than some 32000 or more. 例如, i and J are decalred as integer ,但是它持有的值可能会大于32000或更大。 so try declaring the variable as double or variant . 因此,请尝试将变量声明为double or variant

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM