vba 堆栈空间不足vba excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24504053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:39:08  来源:igfitidea点击:

Out of stack space vba excel

excelvbaexcel-vba

提问by dhruva_04

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'). Can someone please help?

并且我无法解决此错误。我已尝试在线搜索,但似乎无法找到此问题所需的解决方案。它显示“堆栈外”错误(运行时错误“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

回答by dhruva_04

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

回答by Snail

"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. so try declaring the variable as double or variant.

当您分配的变量数量超过它可以容纳的数量时,会导致“堆栈外”错误。例如,i and J are decalred as integer,但是它所持有的价值可能比大约 32000 或更多。所以尝试将变量声明为double or variant.