+ Reply to Thread
Results 1 to 5 of 5

Subscript out of Range

  1. #1
    Jon
    Guest

    Subscript out of Range

    Hey, this seems so simple but I've been staring at this code for an
    hour or so and can't figure out what's wrong. I'm a new VBA
    programmer, so forgive me if this is a really dumb question.

    I keep getting Error 9 - Subscript out of range for this Sub. It
    happens on the first time through the loop, on this line:
    dealercard(cardnum).Picture = images(10).Picture

    I'm not sure why at that point the array index wouldn't be out of
    range, but if you can help, please do. Here's the code (sorry its
    long):

    ---------------------------------------------------------------------------------------

    Private Sub CommandButton1_Click()
    Dim images(1 To 13) As Image
    Dim cardnum, total, house As Integer
    Dim result As String
    Dim dealercard() As Image
    Dim playercard() As Image

    Set images(1) = New Image
    Set images(2) = New Image
    Set images(3) = New Image
    Set images(4) = New Image
    Set images(5) = New Image
    Set images(6) = New Image
    Set images(7) = New Image
    Set images(8) = New Image
    Set images(9) = New Image
    Set images(10) = New Image
    Set images(11) = New Image
    Set images(12) = New Image
    Set images(13) = New Image

    images(1).Picture = imgTwod.Picture
    images(2).Picture = imgThreed.Picture
    images(3).Picture = imgFourd.Picture
    images(4).Picture = imgFived.Picture
    images(5).Picture = imgSixd.Picture
    images(6).Picture = imgSevend.Picture
    images(7).Picture = imgEightd.Picture
    images(8).Picture = imgNined.Picture
    images(9).Picture = imgTend.Picture
    images(10).Picture = imgJackd.Picture
    images(11).Picture = imgQueend.Picture
    images(12).Picture = imgKingd.Picture
    images(13).Picture = imgAced.Picture



    result = vbYes

    Do Until result = vbNo

    house = Int(13 * Rnd()) + 1

    For cardnum = 1 To 7

    If house = 1 Then
    dealercard(cardnum).Picture = images(1).Picture
    house = 2
    ElseIf house = 2 Then
    dealercard(cardnum).Picture = images(2).Picture
    house = 3
    ElseIf house = 3 Then
    dealercard(cardnum).Picture = images(3).Picture
    house = 4
    ElseIf house = 4 Then
    dealercard(cardnum).Picture = images(4).Picture
    house = 5
    ElseIf house = 5 Then
    dealercard(cardnum).Picture = images(5).Picture
    house = 6
    ElseIf house = 6 Then
    dealercard(cardnum).Picture = images(6).Picture
    house = 7
    ElseIf house = 7 Then
    dealercard(cardnum).Picture = images(7).Picture
    house = 8
    ElseIf house = 8 Then
    dealercard(cardnum).Picture = images(8).Picture
    house = 9
    ElseIf house = 9 Then
    dealercard(cardnum).Picture = images(9).Picture
    house = 10
    ElseIf house = 10 Then
    'error occurs at this line
    dealercard(cardnum).Picture = images(10).Picture
    house = 10
    ElseIf house = 11 Then
    dealercard(cardnum).Picture = images(11).Picture
    house = 10
    ElseIf house = 12 Then
    dealercard(cardnum).Picture = images(12).Picture
    house = 10
    ElseIf house = 13 Then
    dealercard(cardnum).Picture = images(13).Picture
    house = 1
    End If

    total = house

    cardnum = cardnum + 1

    result = MsgBox("You have a total of " & total & "." & Chr(13) &
    Chr(13) & "Do you want another card?", vbYesNo, "Total")

    Next

    Loop

    End Sub


  2. #2
    Tom Ogilvy
    Guest

    RE: Subscript out of Range

    I don't see any statement that dimensions dealercard (or playercard either)

    also, if you are looping over cardnum, then why are you doing

    Cardnum = cardnum + 1
    inside the loop?

    Redim DealerCard(1 to 7)

    --
    Regards,
    Tom Ogilvy


    "Jon" wrote:

    > Hey, this seems so simple but I've been staring at this code for an
    > hour or so and can't figure out what's wrong. I'm a new VBA
    > programmer, so forgive me if this is a really dumb question.
    >
    > I keep getting Error 9 - Subscript out of range for this Sub. It
    > happens on the first time through the loop, on this line:
    > dealercard(cardnum).Picture = images(10).Picture
    >
    > I'm not sure why at that point the array index wouldn't be out of
    > range, but if you can help, please do. Here's the code (sorry its
    > long):
    >
    > ---------------------------------------------------------------------------------------
    >
    > Private Sub CommandButton1_Click()
    > Dim images(1 To 13) As Image
    > Dim cardnum, total, house As Integer
    > Dim result As String
    > Dim dealercard() As Image
    > Dim playercard() As Image
    >
    > Set images(1) = New Image
    > Set images(2) = New Image
    > Set images(3) = New Image
    > Set images(4) = New Image
    > Set images(5) = New Image
    > Set images(6) = New Image
    > Set images(7) = New Image
    > Set images(8) = New Image
    > Set images(9) = New Image
    > Set images(10) = New Image
    > Set images(11) = New Image
    > Set images(12) = New Image
    > Set images(13) = New Image
    >
    > images(1).Picture = imgTwod.Picture
    > images(2).Picture = imgThreed.Picture
    > images(3).Picture = imgFourd.Picture
    > images(4).Picture = imgFived.Picture
    > images(5).Picture = imgSixd.Picture
    > images(6).Picture = imgSevend.Picture
    > images(7).Picture = imgEightd.Picture
    > images(8).Picture = imgNined.Picture
    > images(9).Picture = imgTend.Picture
    > images(10).Picture = imgJackd.Picture
    > images(11).Picture = imgQueend.Picture
    > images(12).Picture = imgKingd.Picture
    > images(13).Picture = imgAced.Picture
    >
    >
    >
    > result = vbYes
    >
    > Do Until result = vbNo
    >
    > house = Int(13 * Rnd()) + 1
    >
    > For cardnum = 1 To 7
    >
    > If house = 1 Then
    > dealercard(cardnum).Picture = images(1).Picture
    > house = 2
    > ElseIf house = 2 Then
    > dealercard(cardnum).Picture = images(2).Picture
    > house = 3
    > ElseIf house = 3 Then
    > dealercard(cardnum).Picture = images(3).Picture
    > house = 4
    > ElseIf house = 4 Then
    > dealercard(cardnum).Picture = images(4).Picture
    > house = 5
    > ElseIf house = 5 Then
    > dealercard(cardnum).Picture = images(5).Picture
    > house = 6
    > ElseIf house = 6 Then
    > dealercard(cardnum).Picture = images(6).Picture
    > house = 7
    > ElseIf house = 7 Then
    > dealercard(cardnum).Picture = images(7).Picture
    > house = 8
    > ElseIf house = 8 Then
    > dealercard(cardnum).Picture = images(8).Picture
    > house = 9
    > ElseIf house = 9 Then
    > dealercard(cardnum).Picture = images(9).Picture
    > house = 10
    > ElseIf house = 10 Then
    > 'error occurs at this line
    > dealercard(cardnum).Picture = images(10).Picture
    > house = 10
    > ElseIf house = 11 Then
    > dealercard(cardnum).Picture = images(11).Picture
    > house = 10
    > ElseIf house = 12 Then
    > dealercard(cardnum).Picture = images(12).Picture
    > house = 10
    > ElseIf house = 13 Then
    > dealercard(cardnum).Picture = images(13).Picture
    > house = 1
    > End If
    >
    > total = house
    >
    > cardnum = cardnum + 1
    >
    > result = MsgBox("You have a total of " & total & "." & Chr(13) &
    > Chr(13) & "Do you want another card?", vbYesNo, "Total")
    >
    > Next
    >
    > Loop
    >
    > End Sub
    >
    >


  3. #3
    Jon
    Guest

    Re: Subscript out of Range

    Well, playercard is going to come later in the program, that's why I
    haven't used it.

    Also, at first I was going to control the loop using cardnum = cardnum
    + 1 but changed it and forgot to take it out.

    So, after that out and placing the ReDim dealercard(1 to 7) just inside
    the For loop, it's giving me an error onthe same line stating: "Runtime
    91: Object variable or With block variable not set".

    I apologize again for not being very good with this. Like I said, I'm
    a newbie.


  4. #4
    Jon
    Guest

    Re: Subscript out of Range

    Nevermind, I got it. I had to set all of the dealercards as New
    Images. Thanks for your help.


  5. #5
    Tom Ogilvy
    Guest

    Re: Subscript out of Range

    Doing that is a waste of time.

    --
    Regards,
    Tom Ogilvy

    "Jon" <bickett@gmail.com> wrote in message
    news:1144345964.103895.27210@g10g2000cwb.googlegroups.com...
    > Nevermind, I got it. I had to set all of the dealercards as New
    > Images. Thanks for your help.
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1