【问题标题】:How to fill non-contiguous ranges?如何填充非连续范围?
【发布时间】: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 人一组的方式放置。

标签: excel vba


【解决方案1】:

也许试试这个:

Sub DivideIntoCards(playerArr As Variant)

Dim wb As Workbook
Dim ws As Worksheet

Set wb = ThisWorkbook
Set ws = wb.ActiveSheet

Const PLAYER_PER_CARD = 3
Const START_ROW = 12
Const CARD_OFFSET = 7 'offset rows

cols = Array(11, 16) 'set predefined columns
    
players = UBound(playerArr) - LBound(playerArr) + 1

If players Mod PLAYER_PER_CARD = 0 Then
    
    cardCount = CInt(players / PLAYER_PER_CARD) - 1
    rPL = START_ROW
    
    For card = 0 To cardCount
        
        m = card Mod 2 'determine odd/even card
        If m = 0 Then rPL = START_ROW + (card / 2) * CARD_OFFSET 'increase row on uneven cards
        
        cPL = cols(m) 'choose correct column, based on odd/even card
                    
        For i = 0 To PLAYER_PER_CARD - 1
            plIndex = card * PLAYER_PER_CARD + i
            ws.Cells(rPL + i, cPL) = playerArr(plIndex)
        Next i
        
    Next
Else

    Response = MsgBox("The player count of " & players & _
    " cannot be divided in equals groups of " & PLAYER_PER_CARD & _
    " players.", vbCritical, "Player count Error")
End If

End Sub

【讨论】:

  • 这段代码效果很好。我想我将能够使它适应我正在编写的这个特定程序中发生的其他几种情况。谢谢你! (我试着给你一个积极的声誉,但它不会让我)
  • 很高兴我能提供帮助,如果它回答了您的问题,只需将答案标记为正确答案以帮助 SO 保持正确(即减少未回答问题的数量)
猜你喜欢
  • 2013-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-15
  • 2019-09-05
  • 2021-10-12
  • 2022-12-29
  • 2017-07-16
相关资源
最近更新 更多