Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Subscript out of Range

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

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Subscript out of Range

Doing that is a waste of time.

--
Regards,
Tom Ogilvy

"Jon" wrote in message
ups.com...
Nevermind, I got it. I had to set all of the dealercards as New
Images. Thanks for your help.





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subscript Out of Range ExcelMonkey[_190_] Excel Programming 6 February 20th 05 02:46 AM
Subscript out of range.... Edgar Thoemmes[_4_] Excel Programming 4 January 13th 05 02:06 AM
Subscript out of range teresa Excel Programming 1 December 14th 04 09:56 PM
Subscript out of Range Q John Excel Programming 5 December 14th 04 07:54 AM
Subscript out of range Stacy Haskins[_2_] Excel Programming 4 April 10th 04 05:41 AM


All times are GMT +1. The time now is 12:19 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"