Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA单元格,将带分隔符的值添加到列表中_Excel_Vba - Fatal编程技术网

Excel VBA单元格,将带分隔符的值添加到列表中

Excel VBA单元格,将带分隔符的值添加到列表中,excel,vba,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 (是的,他们使用了多种分隔符) 我只需要将第4单元拆分成新行,让第1-3单元位于每一行 a b c 1 b b

我有一张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
(是的,他们使用了多种分隔符)

我只需要将第4单元拆分成新行,让第1-3单元位于每一行

a       b      c      1
b       b      c      2
etc...
我试着录制用于拆分文本和转置的宏,但我无法理解新行的插入等。
需要帮忙吗

使用此选项插入一个空行

ActiveSheet.Cells(2, 1).EntireRow.Resize(1).Insert
完全解

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

张贴您录制的代码。告诉我们哪里出了问题,什么地方出了问题,这样我们就可以帮助你解决问题。这就是如何使用@MikeH:这段代码现在起作用了,我在ubound中犯了一个错误,这就是为什么破折号分隔符不起作用的原因——它只有两个元素。难以置信的Daniel,我来试一试,我在c=Split(Cells(rowCurrent,4),“,”)行上遇到了一个编译错误,但我很确定我能解决这个问题。谢谢好的,我刚刚把它直接复制到一本新的工作簿中,效果很好。如果这有什么不同,我会使用Excel 2010。祝你好运。就这样,我又试了一次,成功了——你是明星,谢谢
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