0

我有一张需要操作的 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...

我已经尝试记录用于分割文本和转置的宏,但我无法理解插入新行等。请帮忙吗?

4

2 回答 2

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
于 2013-06-26T16:03:39.357 回答
0

使用它插入一个空白行

ActiveSheet.Cells(2, 1).EntireRow.Resize(1).Insert
于 2013-06-26T15:24:04.980 回答