Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.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
Vba Excel宏-逗号分隔的单元格到不同工作表的行_Vba_Excel - Fatal编程技术网

Vba Excel宏-逗号分隔的单元格到不同工作表的行

Vba Excel宏-逗号分隔的单元格到不同工作表的行,vba,excel,Vba,Excel,我的excel里有两张表格。在我的excel中有以下数据 在第一页,我有 。。。。。。。等## 对于每一行,表示一行和一个单元格 我想将其转换为: 1. (column 1) a (column 2) p 2. (column 1) a (column 2) q 3. (column 1) a (column 2) r 4. (column 1) a (column 2) s 5. (column 1) a (c

我的excel里有两张表格。在我的excel中有以下数据 在第一页,我有


。。。。。。。等##


对于每一行,表示一行和一个单元格

我想将其转换为:

 1. (column 1) a   (column 2) p 
 2. (column 1) a   (column 2) q   
 3. (column 1) a   (column 2) r 
 4. (column 1) a   (column 2) s 
 5. (column 1) a                      (column 3) u 
 6. (column 1) a                      (column 3) v 
 7. (column 1) a                      (column 3) w 
 8. (column 1) a                                          (column 4) x 
 9. (column 1) a                                          (column 4) y 
 10.(column 1) a                                          (column 4) z
试试下面的代码

Sub test()
    Dim lastrow, lastcolumn, i, j, k As Long
    Dim str1, str2
    lastrow = Range("A" & Rows.Count).End(xlUp).Row
    lastcolumn = Cells(1, Columns.Count).End(xlToLeft).Column
    For i = 1 To lastrow
        For j = 2 To lastcolumn
            str1 = Split(Cells(i, j), ",")
            str2 = UBound(str1)
            For k = 0 To str2
                With Sheets("Sheet2")
                    .Range("A" & .Range("A" & Rows.Count).End(xlUp).Row + 1).Value = Cells(i, 1)
                    .Range("B" & .Range("B" & Rows.Count).End(xlUp).Row + 1).Value = str1(k)
                End With
            Next k
        Next j
    Next i
End Sub
工作证明


@Poorni,永远欢迎你
Sub test()
    Dim lastrow, lastcolumn, i, j, k As Long
    Dim str1, str2
    lastrow = Range("A" & Rows.Count).End(xlUp).Row
    lastcolumn = Cells(1, Columns.Count).End(xlToLeft).Column
    For i = 1 To lastrow
        For j = 2 To lastcolumn
            str1 = Split(Cells(i, j), ",")
            str2 = UBound(str1)
            For k = 0 To str2
                With Sheets("Sheet2")
                    .Range("A" & .Range("A" & Rows.Count).End(xlUp).Row + 1).Value = Cells(i, 1)
                    .Range("B" & .Range("B" & Rows.Count).End(xlUp).Row + 1).Value = str1(k)
                End With
            Next k
        Next j
    Next i
End Sub