检查数组VBA的值

我试图确保我build立的数组具有我期待的值。

Cardsarrays应该填充Shoe数组。

当我计算数组的值时,我得到了预期的104,但是当我将这些值粘贴到Excel表格中时,只有13个单元格填充。

有没有简单的方法来检查数组的内容?

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 

你只是在这里填入前13个值:

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

你将要循环8次得到104

你想做什么? 用104张牌(每个8张)填充鞋? 如果是的话,我会用split / join方法来做。 如果你需要,很高兴发布一些代码。 这个想法是在一个循环上build立一个string,以根据需要多次添加连接的卡片数组(在这种情况下是8),然后将其拆分成鞋。

你已经通过丢弃套装来均化牌,但是他们仍然需要被添加到循环中以获得每个套牌的正确数量的牌。

 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 

我已经包括一个可选的代码行,包括每个卡正在构build的套装。 为了debugging目的,可能会更好,直到你的鞋子生成正确,然后在运行后丢弃。

你可以使用Join

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

哪个打印

 North, South, East, West 

在即时窗口中。 另外,如果您使用debugging器来遍历代码,那么在本地窗口中,您可以展开数组的名称并检查单个元素。

在编辑:扩大最后一点。 如果在VBA编辑器中selectView -> Locals Window并在发送shoe到工作表的代码行之前设置一个断点(通过单击一行的左侧),您应该看到如下所示的内容:

在这里输入图像描述

如果你运行的子进入rest模式像这样:

在这里输入图像说明

那么如果你扩大shoe你会看到:

在这里输入图像说明

这足以certificate你没有在索引13之后初始化shoe 。其他人已经发布了答案,显示了这个 bug的来源 – 但是当你的问题被问到时,确实是一个非常重要的技能,可以检查一个数组的值。