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

时间:2018-04-03 11:06:41

标签: excel vba excel-vba excel-formula

假设我有像

这样的数据
1
1
1
1
2
2
3
3
3
3
4
5
6
6
.
.
.

我希望将其分隔为

1
 1
  1
   1
2
 2
3
 3
  3
   3
4
5
6
 6

请帮帮我,我会非常感谢你。

4 个答案:

答案 0 :(得分: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

这会产生以下输出:

A    will become     A | B | C
1                    1 |   |
1                      | 1 |
1                      |   | 1
2                    2 |   |
2                      | 2 |
3                    3 |   |

答案 1 :(得分:3)

如果您在 Sheet1 中有数据,请执行以下操作:

enter image description here

选择数据并运行此宏:

const arrayOfConfigs = config.util.getConfigSources()
const loadedConfigPath = arrayOfConfigs[arrayOfConfigs.length - 1].name

将在 Sheet2

上生成此内容

enter image description here

答案 2 :(得分:0)

一般情况下,当您在StackOverflow中提出问题时,始终建议您提供一些代码(或您尝试过的代码): startup

尽管如此,如果你认为输入应来自一个单元并返回下一个单元格,这是另一种可能的解决方案:

general.scss

Function CharacterArray(value As String) As Variant

        Dim cnt         As Long
        Dim result      As Variant
        Dim someVal     As String

        For cnt = 1 To Len(value)
            someVal = Mid(value, cnt, 1)
            If someVal Like WorksheetFunction.Rept("[^a-zA-Z0-9.]", 1) Then
                If IsEmpty(result) Then
                    ReDim result(1)
                Else
                    ReDim Preserve result(UBound(result) + 1)
                End If
                result(UBound(result)) = Mid(value, cnt, 1)
            End If
        Next cnt

        CharacterArray = result

End Function

和CharacterArray:

A1

实际上,此解决方案的最大“问题”是您必须找到一种方法将范围For cnt = 1 To Len(value) someVal = Mid(value, cnt, 1) If someVal Like WorksheetFunction.Rept("[^a-zA-Z0-9.]", 1) Then 中的值拆分为数组。这是通过以下3行完成的,这些行从新数组中删除新行:

If resultArray(cnt) = resultArray(cnt - 1) Then

一旦你有一个包含你需要的值的漂亮数组,你只需要像这样比较循环中的当前值和前一个值:

spaces

并将spaces = spaces & " "增加为1,如果它们相同:

$( "#target2" ) .mouseover(function() { console.log("mouse is inside target2"); })

答案 3 :(得分:0)

我用字典来完成这项任务

Option Explicit

Sub Multiple()
Dim rg As Range
Dim i As Long
Dim dict As Object
Dim vdat As Variant

    Set rg = Range("A1:A12")
    Set dict = CreateObject("Scripting.Dictionary")

    vdat = WorksheetFunction.Transpose(rg)

    For i = LBound(vdat) To UBound(vdat)
        If dict.Exists(vdat(i)) Then
            dict.Item(vdat(i)) = dict.Item(vdat(i)) + 1
        Else
            dict.Add vdat(i), 1
        End If
    Next i

    Set rg = Range("B1")

    Dim key As Variant
    Dim rowNo As Long

    rowNo = 0

    For Each key In dict
        For i = 1 To dict(key)
            rg.Cells(rowNo + i, i).Value = key
        Next
        rowNo = rowNo + dict(key)
    Next

End Sub

PS 基于有关字典中键的顺序的讨论。 上面的代码只会将不同键的数量放在不同的行和列中。它不会考虑列表的顺序也不会考虑键的顺序。您将获得未排序列表的以下结果

enter image description here

如果您预期以下结果,代码将不会为您提供。

enter image description here