"Subscript out of range", in VBA function?

343GS

New Member
Joined
Jun 29, 2017
Messages
14
Happy sunday everybody!
I am new to vba and I am trying to create a deck of 40 cards (mazzo2), with suits (seme) and values (valore):

Code:
Function NtoCarta40() As String
Dim seme(1 To 40) As String, valore(1 To 40) As Long, i As Long, mazzo2(1 To 40) As String

For i = 1 To 40

Select Case i
    Case i = 1 To 10
    seme(i) = "C"
    Case i = 11 To 20
    seme(i) = "P"
    Case i = 21 To 30
    seme(i) = "Q"
    Case i = 31 To 40
    seme(i) = "F"
End Select
Next

For i = 1 To 40
Select Case i
    Case i = 1 To 10
    valore(i) = i
    Case i = 11 To 20
    valore(i) = i - 10
    Case i = 21 To 30
    valore(i) = i - 20
    Case i = 31 To 40
    valore(i) = i - 30
End Select
Next
 
For i = 1 To 40
    mazzo2(i) = valore(i) & " " & seme(i)
Next
NtoCarta40 = mazzo2(i)

End Function


The problem is, after the last "Next" instruction I get error "Subscript out of range"......can someone help me to understand why?

Thank you!
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
The problem is, after the last "Next" instruction I get error "Subscript out of range"......can someone help me to understand why?
Yes, that final 'Next' increases i to 41 which is beyond the upper bound of mazzo2 which has an upper bound of 40
 
Last edited:
Upvote 0
why not list all cards in the deck in column A and a set of random numbers in column B, then sort ascending on column B ?
 
Upvote 0
why not list all cards in the deck in column A and a set of random numbers in column B, then sort ascending on column B ?

Thank you for the suggestion, but I don't understand the purpose exactly. Could you explain better please?
 
Upvote 0
but why does he do it.....? I mean I set i=1 to 40.....
That is what happens in all loops. In your example, i eventually gets increased to 41 & when vba checks that against the For i = 1 to 40 it determines that i is now too large and does not return to the start of the loop.

Test by running this code & check the results.
Code:
Sub ForNextEndValues()
  Dim i As Long, k As Long, Z As Long
  
  For i = 1 To 5
    'Do nothing
  Next i
  For k = 17 To 11 Step -2
    'Do nothing
  Next k
  For Z = 5 To 100
    'Do nothing
  Next Z
  MsgBox "i = " & i & vbLf & "k = " & k & vbLf & "Z = " & Z
End Sub
 
Last edited:
Upvote 0
pretend a deck of cards is the numbers 1 to 9
put a random number against each card
10.507108
20.909449
30.906568
40.612766
50.190419
60.315807
70.755895
80.852927
90.804632
now sort the table by the right hand column
50.190419
60.315807
10.507108
40.612766
70.755895
90.804632
80.852927
30.906568
20.909449
the deck is now shuffled

<colgroup><col width="64" span="9" style="width:48pt"> </colgroup><tbody>
</tbody>
 
Upvote 0

Forum statistics

Threads
1,214,567
Messages
6,120,268
Members
448,953
Latest member
Dutchie_1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top