我有3列,每列有7行数字。 我的代码应该是:
然而,似乎每次都有某种随机顺序。
这是我第一次使用收藏品。谁能发现我做错了什么?
示例数据
应该发生什么
实际发生的事情
当然,罪魁祸首
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
答案 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)
对于我的下一个魔法技巧,我将使所有For row = 1 To 7
消失
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
现在它按预期工作。