如何从不同的不同列中的一列中分离相同的数字?

Aks*_*hav 0 excel vba excel-vba excel-formula

假设我有像这样的数据

1
1
1
1
2
2
3
3
3
3
4
5
6
6
.
.
.
Run Code Online (Sandbox Code Playgroud)

我想把它分开

1
 1
  1
   1
2
 2
3
 3
  3
   3
4
5
6
 6
Run Code Online (Sandbox Code Playgroud)

请帮帮我,我将非常感谢你.

Mic*_*zyn 7

试试这段代码:

Sub SplitToColumns()
Dim numbers As Variant, lastRow As Long, i As Long, col As Long
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
col = 1
'read all column A (I assumed you store these numbers there)
numbers = Range("A1:A" & lastRow).Value2
'clear column A, as we will write here
Range("A1:A" & lastRow).Clear

Cells(1, 1).Value = numbers(1, 1)
For i = 2 To lastRow
    'if the value is the same as previous one, we will write in next column
    If numbers(i, 1) = numbers(i - 1, 1) Then
        col = col + 1
    'if number is different from previous, then write in first column (A)
    Else
        col = 1
    End If
    Cells(i, col).Value = numbers(i, 1)
Next
End Sub
Run Code Online (Sandbox Code Playgroud)

这会生成以下输出:

A    will become     A | B | C
1                    1 |   |
1                      | 1 |
1                      |   | 1
2                    2 |   |
2                      | 2 |
3                    3 |   |
Run Code Online (Sandbox Code Playgroud)