Fubudis
Fubudis

Reputation: 251

Checking the values of an array VBA

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

Upvotes: 2

Views: 17069

Answers (3)

John Coleman
John Coleman

Reputation: 51988

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.

Upvotes: 6

user4039065
user4039065

Reputation:

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.

Upvotes: 1

Dan Donoghue
Dan Donoghue

Reputation: 6186

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.

Upvotes: 2

Related Questions