【发布时间】:2023-02-09 19:08:18
【问题描述】:
我有一个用于管理联赛的电子表格。我正在重写整个练习。
有没有办法缩短重复循环?
签到的人在 B 列中有他们的名字。签到完成后,我用他们的名字填充一个数组,随机化,然后将它们放在右侧显示的卡片上。
Sub DivideIntoCards(playerArr As Variant)
Dim i, j As Integer
Dim remainder As Integer
With ActiveSheet
remainder = UBound(playerArr) - LBound(playerArr) + 1
If remainder Mod 4 = 0 Then
'Number of players checked in creates equal cards of 4.
Do Until remainder = 0
j = 0
'Fill card #1
If i < 4 Then
For i = 0 To 3
Cells(12 + j, 11) = playerArr(i)
remainder = remainder - 1
j = j + 1
Next i
'Fill card #2
ElseIf 4 <= i And i < 8 Then
For i = 4 To 7
Cells(12 + j, 16) = playerArr(i)
remainder = remainder - 1
j = j + 1
Next i
'Fill card #3
ElseIf 8 <= i And i < 12 Then
For i = 8 To 11
Cells(19 + j, 11) = playerArr(i)
remainder = remainder - 1
j = j + 1
Next i
'Fill card #4
ElseIf 12 <= i And i < 16 Then
For i = 12 To 15
Cells(19 + j, 16) = playerArr(i)
remainder = remainder - 1
j = j + 1
Next i
'Fill card #5
ElseIf 16 <= i And i < 20 Then
For i = 16 To 19
Cells(26 + j, 11) = playerArr(i)
remainder = remainder - 1
j = j + 1
Next i
'Fill card #6
ElseIf 20 <= i And i < 24 Then
For i = 20 To 23
Cells(26 + j, 16) = playerArr(i)
remainder = remainder - 1
j = j + 1
Next i
'Fill card #7
ElseIf 24 <= i And i < 28 Then
For i = 24 To 27
Cells(33 + j, 11) = playerArr(i)
remainder = remainder - 1
j = j + 1
Next i
'Fill card #8
ElseIf 28 <= i And i < 32 Then
For i = 28 To 31
Cells(33 + j, 16) = playerArr(i)
remainder = remainder - 1
j = j + 1
Next i
'Fill card #9
ElseIf 32 <= i And i < 36 Then
For i = 32 To 35
Cells(40 + j, 11) = playerArr(i)
remainder = remainder - 1
j = j + 1
Next i
'Fill card #10
ElseIf 36 <= i And i < 40 Then
For i = 36 To 39
Cells(40 + j, 16) = playerArr(i)
remainder = remainder - 1
j = j + 1
Next i
'Fill card #11
ElseIf 40 <= i And i < 44 Then
For i = 40 To 43
Cells(47 + j, 11) = playerArr(i)
remainder = remainder - 1
j = j + 1
Next i
'Fill card #12
ElseIf 44 <= i And i < 48 Then
For i = 44 To 47
Cells(47 + j, 16) = playerArr(i)
remainder = remainder - 1
j = j + 1
Next i
End If
Loop
End If
End With
End Sub
【问题讨论】:
-
看起来你在一个大阵列中拥有每个玩家的所有“卡片”?这就是为什么你需要不断增加
i你可以尝试做一个数组数组? -
@mtholen 我在传递给这个子的数组中有每个玩家的真实姓名。这个子然后将它们排列在图片右侧的卡片中。不过,我从来没有制作过数组,如果每张卡片本身就是一个数组,这可能会起作用。我会调查一下,看看我能否得到一些工作。截至目前,卡片尚未分配给任何类型的变量。 for 循环遍历每张卡片,并将卡片中的人以 4 人一组的方式放置。