2

I'm trying to make sure the array I've built has the values I'm expecting.

The Cards array is supposed to populate the Shoe array.

When I count the values of the array, I get the expected 104, but when I paste the values into an excel sheet, only 13 cells populate.

Is there an easy way to check the contents of an array?

Sub CreateShoe()

Dim decks As Integer
decks = 2


Dim Cards As Variant
Dim shoe As Variant
Dim cnt As Integer


Cards = Array(2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, "A")
ReDim shoe(1 To 52 * decks)

cnt = 1

For i = LBound(Cards) To UBound(Cards)
shoe(cnt) = Cards(i)
cnt = cnt + 1
Next i

Range("A1:A99") = WorksheetFunction.Transpose(shoe)

End Sub
2
  • In your for loop, you can print the current array value to the debug window (aka Immediate Window). Just add Debug.print Cards(i). That'll help determine what is in the cards array. I'm trying to work through your macro, but what's the ultimate goal? To output the shoe array to Range A1. What's the Shoe array to consist of? Your shoe currently holds (1 to 104). As far as I can tell, you never populate it 54 times? Commented Aug 17, 2015 at 21:15
  • I'm trying to create a shoe of cards you might find at a casino. In this case I want the shoe to contain two regular decks of cards. I've normalized the 10, jack, queen, and king to 10s and haven't included suits because I'm going to be using this for a blackjack simulation. Commented Aug 17, 2015 at 21:27

3 Answers 3

6

You can use Join:

Sub test()
    Dim Directions As Variant
    Directions = Array("North", "South", "East", "West")
    Debug.Print Join(Directions, ", ")
End Sub

Which prints

North, South, East, West

in the Immediate Window. Also, if you are using the debugger to step through the code then in the Locals Window you can expand on the name of the array and inspect the individual elements.

On edit: expanding on this last point. If in the VBA editor you pick View -> Locals Window and set a breakpoint (by clicking to the left of a line) before the line of code where you send shoe to the worksheet, you should see something like:

enter image description here

If you run the sub it will go into break mode like thus:

enter image description here

Then if you expand on shoe you will see:

enter image description here

Which is enough to show that you are not initializing shoe after index 13. Others have posted answers showing the source of this bug -- but it really is an important skill to be able to, as your question asked, check the values of an array.

On Edit Join doesn't play well with arrays that are neither Variant nor String. If you have an array that is declared like e.g.

Dim Directions(1 to 4) as Integer

you could first run it through the following function:

Function ToVariant(v As Variant) As Variant
    Dim temp As Variant
    Dim i As Long
    ReDim temp(LBound(v) To UBound(v))
    For i = LBound(v) To UBound(v)
        temp(i) = v(i)
    Next i
   ToVariant = temp
End Function

Then Join(ToVariant(Directions), ", ") will work as expected.

Sign up to request clarification or add additional context in comments.

4 Comments

Good explanation for debugging and showing the array contents. However, the debug.print example at the beginning of your post doesn't seem to work for other data types except string. Could you provide the code for debug.print an array of integers? (Since that is what the OP has as well.)
@Nelda.techspiress The problem isn't String vs Integer so much as Variant vs non-variant. Variant is by far the most flexible datatype when working with arrays. See if the edit at the bottom of the post helps.
So since there is no reason for my data to be defined as Integer, I should just define it as Variant and have more flexibility in my code. ;-)
@Nelda.techspiress There are pros and cons. Variants have an overhead so there might be a performance hit plus the relative lack of type-checking could mask a bug. On the other hand, they are undoubtedly more flexible.
2

You are only populating the first 13 values here:

For i = LBound(Cards) To UBound(Cards)
shoe(cnt) = Cards(i)
cnt = cnt + 1
Next i

You will want to loop that 8 times to get 104

What are you trying to do? Fill shoe with 104 cards (8 of each)? If so I would do it with a split / join method. Happy to post some code for this if you need. The idea is build a string on a loop to append the joined array of Cards as many times as you need (in this case 8) then split that out to shoe.

Comments

1

You've homogenized the cards by discarding the suits but they still need to be added to the loops to get the correct number of cards for each deck.

Sub CreateShoe()

    Dim Cards As Variant, suits As Variant, shoe As Variant
    Dim decks As Long, i As Long, d As Long, s As Long

    decks = 2
    suits = Array("H", "D", "S", "C")
    Cards = Array(2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A")
    ReDim shoe(1 To (52 * decks))

    For d = 1 To decks
        For s = LBound(suits) To UBound(suits)
            For i = LBound(Cards) To UBound(Cards)
                shoe(1 + i + (s * (UBound(Cards) + 1)) + ((d - 1) * 52)) = Cards(i)
                'optionally include the suit
                'shoe(1 + i + (s * (UBound(Cards) + 1)) + ((d - 1) * 52)) = Cards(i) & suits(s)
            Next i
        Next s
    Next d

    Range("A1").Resize(UBound(shoe), 1) = WorksheetFunction.Transpose(shoe)

End Sub

I've included an optional code line that includes the suit of each card as it is being built. It may be better to include that for debugging purposes until you get your shoe generation correct and then discard the suit after it is running.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.