未能使用馆藏

时间:2016-11-24 18:19:54

标签: excel vba excel-vba

我有3列,每列有7行数字。 我的代码应该是:

  1. 确定我选择的列
  2. 从我未选择的其中一个列中获取所有数字并添加到集合中(按顺序。这很重要)
  3. 从我选择的列中获取所有数字并将其添加到集合中(也按顺序)
  4. 从剩余列中获取所有数字并将其添加到集合中。
  5. 按顺序,从第一列到最后一列,从第一行到最后一行,放置集合内的项目
  6. 然而,似乎每次都有某种随机顺序。

    这是我第一次使用收藏品。谁能发现我做错了什么?

    示例数据

    Sample Data

    应该发生什么

    What should happen

    实际发生的事情

    What actually happens

    当然,罪魁祸首

    Option Explicit
    Public vez As Long
    Public grupo As New Collection
    
    Sub magic(coluna As Long)
    Dim row As Long
    Dim col As Long
    Dim i As Long
    Set grupo = Nothing
    If vez < 3 Then
        If coluna = 1 Then
            For row = 1 To 7
                grupo.Add Cells(row, 2)
            Next row
            For row = 1 To 7
                grupo.Add Cells(row, 1)
            Next row
            For row = 1 To 7
                grupo.Add Cells(row, 3)
            Next row
        ElseIf coluna = 2 Then
            For row = 1 To 7
                grupo.Add Cells(row, 1)
            Next row
            For row = 1 To 7
                grupo.Add Cells(row, 2)
            Next row
            For row = 1 To 7
                grupo.Add Cells(row, 3)
            Next row
        ElseIf coluna = 3 Then
            For row = 1 To 7
                grupo.Add Cells(row, 2)
            Next row
            For row = 1 To 7
                grupo.Add Cells(row, 3)
            Next row
            For row = 1 To 7
                grupo.Add Cells(row, 1)
            Next row
        End If
        i = 1
        For row = 1 To 7
            For col = 1 To 3
                Cells(row, col) = grupo.Item(i)
                i = i + 1
            Next col
        Next row
        vez = vez + 1
    End If
    End Sub
    

3 个答案:

答案 0 :(得分:1)

您要向Cell添加Collection,但我认为您确实要添加Cell Value(以便使用其中的值重写将单元格添加到集合时的单元格,而不是在将单元格写入新位置时写出单元格中的值。“

Option Explicit
Public vez As Long
Public grupo As New Collection

Sub magic(coluna As Long)
    Dim row As Long
    Dim col As Long
    Dim i As Long
    Set grupo = Nothing
    If vez < 3 Then
        If coluna = 1 Then
            For row = 1 To 7
                grupo.Add Cells(row, 2).Value
            Next row
            For row = 1 To 7
                grupo.Add Cells(row, 1).Value
            Next row
            For row = 1 To 7
                grupo.Add Cells(row, 3).Value
            Next row
        ElseIf coluna = 2 Then
            For row = 1 To 7
                grupo.Add Cells(row, 1).Value
            Next row
            For row = 1 To 7
                grupo.Add Cells(row, 2).Value
            Next row
            For row = 1 To 7
                grupo.Add Cells(row, 3).Value
            Next row
        ElseIf coluna = 3 Then
            For row = 1 To 7
                grupo.Add Cells(row, 2).Value
            Next row
            For row = 1 To 7
                grupo.Add Cells(row, 3).Value
            Next row
            For row = 1 To 7
                grupo.Add Cells(row, 1).Value
            Next row
        End If
        i = 1
        For row = 1 To 7
            For col = 1 To 3
                Cells(row, col).Value = grupo.Item(i)
                i = i + 1
            Next col
        Next row
        vez = vez + 1
    End If
End Sub

答案 1 :(得分:1)

enter image description here

对于我的下一个魔法技巧,我将使所有For row = 1 To 7消失

Alakazam !!!!

Option Explicit
Public vez As Long
Public grupo As New Collection

Sub magic(coluna As Long)
    Dim i As Long
    Dim c As Range, Source As Range
    Set Source = Range(Choose(coluna, "B1:B7,A1:A7,C1:C7", "A1:A7,B1:B7,C1:C7", "B1:B7,C1:C7,A1:A7"))
    Set grupo = New Collection
    For Each c In Source
        grupo.Add c.Value
    Next

    For Each c In Source
        grupo.Add c.Value
    Next

    For Each c In Range("A1:C7")
        i = i + 1
        c.Value = grupo.Item(i)
    Next

    vez = vez + 1
End Sub

答案 2 :(得分:-1)

编辑:检查@ YowE3K答案。它与集合而不是数组完美配合。

用数组替换集合。数组似乎更容易使用。

Option Explicit
Public vez As Long


Sub magic(coluna As Long)
Dim grupo(1 To 21) As String
Dim row As Long
Dim col As Long
Dim i As Long
i = 1
If vez < 2 Then
    If coluna = 1 Then
        For row = 1 To 7
            grupo(i) = Cells(row, 3).Value
            i = i + 1
        Next row
        For row = 1 To 7
            grupo(i) = Cells(row, 1).Value
            i = i + 1
        Next row
        For row = 1 To 7
            grupo(i) = Cells(row, 2).Value
            i = i + 1
        Next row
    ElseIf coluna = 2 Then
        For row = 1 To 7
            grupo(i) = Cells(row, 1).Value
            i = i + 1
        Next row
        For row = 1 To 7
            grupo(i) = Cells(row, 2).Value
            i = i + 1
        Next row
        For row = 1 To 7
            grupo(i) = Cells(row, 3).Value
            i = i + 1
        Next row
    ElseIf coluna = 3 Then
        For row = 1 To 7
            grupo(i) = Cells(row, 2).Value
            i = i + 1
        Next row
        For row = 1 To 7
            grupo(i) = Cells(row, 3).Value
            i = i + 1
        Next row
        For row = 1 To 7
            grupo(i) = Cells(row, 1).Value
            i = i + 1
        Next row
    End If
    i = 1
    For row = 1 To 7
        For col = 1 To 3
            Cells(row, col).Value = grupo(i)
            i = i + 1
        Next col
    Next row
    vez = vez + 1
End If
End Sub

现在它按预期工作。