将一行拆分为多行

时间:2016-03-02 16:18:16

标签: excel ms-office

我有一行包含5000个单元格,我想将这些数据分成几行(每行4个单元格)。

有自动方式吗?

实施例: 由此 123412341234

对此  1234
 1234
 1234

使用的程序是Excel

问候

1 个答案:

答案 0 :(得分:1)

如果您连续有5000个单元格并且您希望每4个单元格拆分并输出一列,则此代码将起作用。

Sub splitInCols()
Dim inputRow As Long, nCol As Long, lastCol As Long, iRow As Long
Dim sht As Worksheet
Set sht = ActiveSheet

inputRow = 1 'Put your row number here
outputCol = 1 'The column where it will output
lastCol = sht.Cells(inputRow, sht.Columns.Count).End(xlToLeft).Column

iRow = 2    'Row start of your output
n = 0
For j = 1 To lastCol
    If n < 4 Then
        splitText = splitText & Cells(inputRow, j).Value
        n = n + 1
    End If
    If n = 4 Or j = lastCol Then
        Cells(iRow, outputCol).Value = splitText
        iRow = iRow + 1
        n = 0
        splitText = ""
    End If
Next
End Sub