带有分隔值的Excel VBA单元格列表

时间:2013-06-26 15:09:36

标签: excel vba

我有一张需要操作的Excel表格。我知道一点VBA,但还不足以实现自动化。

电子表格采用以下格式

Cell1   Cell2  Cell3  Cell4
a       b      c      1, 2, 3, 4, 5, 6
a       d      e      1 3 5 
a       f      g      7-8

(是的,他们使用了各种分隔符)

我只需将Cell4拆分为新行,并将Cells1-3拆分为每行。

a       b      c      1
b       b      c      2
etc...

我已经尝试录制用于分割文本和转置的宏,但是我无法绕过新行的插入等。 有什么帮助吗?

2 个答案:

答案 0 :(得分:0)

使用此项插入一个空白行

ActiveSheet.Cells(2, 1).EntireRow.Resize(1).Insert

答案 1 :(得分:0)

完整的解决方案

Sub newmac()

    Dim rowCount As Long
    Dim rowOffset As Long
    Dim rowCurrent As Long
    Dim subRow As Long
    Dim c() As String

    rowOffset = 0
    rowCount = Application.ActiveSheet.UsedRange.Rows.CountLarge

    For i = 1 To rowCount
        rowCurrent = i + rowOffset
        c = Split(Cells(rowCurrent, 4), ",")
        If (UBound(c) <= 0) Then c = Split(Cells(rowCurrent, 4), " ")
        If (UBound(c) <= 0) Then c = Split(Cells(rowCurrent, 4), "-")
        ' more than 1 item. process
        If (UBound(c) > 0) Then

            Cells(rowCurrent, 4) = c(0)

            For j = 1 To UBound(c)
                subRow = rowCurrent + j
                range("A" & rowCurrent & ":D" & rowCurrent).Copy
                range("A" & subRow & ":D" & subRow).Insert
                Cells(subRow, 4) = c(j)
            Next j
            rowOffset = rowOffset + UBound(c)
        End If
    Next i

End Sub

我的结果:

a   b   c   1
a   b   c   2
a   b   c   3
a   b   c   4
a   b   c   5
a   b   c   6
a   d   e   1
a   d   e   3
a   d   e   5
a   f   g   7
a   f   g   8