Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

excel - Split one row into several rows

I have one row that contains 5000 cells and I want to split this data into several rows (4 cells per row).

Is there an automated way to do it?

Example: From this 123412341234

To this
1234
1234
1234

Program used is Excel

regards

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This code will work if you have 5000 cells in a row and you want to split at every 4 cells and output in a column.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...