dhruva_04
dhruva_04

Reputation: 141

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'). 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

Upvotes: 1

Views: 18072

Answers (2)

Snail
Snail

Reputation: 89

"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.

Upvotes: 0

dhruva_04
dhruva_04

Reputation: 141

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

Upvotes: 1

Related Questions