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
1.1k views
in Technique[技术] by (71.8m points)

excel - Stack multiple columns into two colums in pairs of two

I have columns A:ALC filled with data, and there is a varied number of rows for each column. If possible, I need a macro that will stack columns in pairs of two. For example, column C directly under column A and column D directly under column B and so on for all columns A:ALC.

        COLUMN A    COLUMN B          COLUMN C      COLUMN D
ROW 1   2598        F800              2599          F800
ROW 2   2598        K1300             2599          K1300
ROW 3   2598        S1000RR           2599          R900
ROW 4   2598        G650              2599          G650
ROW 5   2598        R1200             2599          K1600
ROW 6   2599        S1000
ROW 7   2599        HP2
ROW 8   2599        R1200

There is equal amount of data for each pair of columns (e.g. column A and B have 8 rows, column C and D have 5 rows, and so on), but the number of rows, obviously, differ between the many pairs of columns. There are no blanks trapped inside the data.

When I ran the macro you gave Excel displayed this:

Run-Time Error '13': Type Mismatch

What could be the issue?

NOTE: There are some columns with only a pair of data, that is data only in the first row.

This is what I need the output to look like:

       COLUMN A    COLUMN B          
ROW 1   2598        F800              
ROW 2   2598        K1300             
ROW 3   2598        S1000RR 
ROW 4   2598        G650              
ROW 5   2598        R1200             
ROW 6   2599        S1000
ROW 7   2599        HP2
ROW 8   2599        R1200
ROW 9   2599        F800
ROW 10  2599        K1300
ROW 11  2599        R900
ROW 12  2599        G650
ROW 13  2599        K1600
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

If your data range from A:ALC is full, then this variant array code will very quickly form your new range in columns A and B

Note the caveat re full, the code will fail if it encounters a blank or single cell column as a variant array cant be creaed. If this is the case then I will need to add range testing, so pls advise.

[Updated to handle blank ranges and/or single cells]

Sub Combine()
Dim OrigA
Dim OrigB
Dim strA As String
Dim strB As String
Dim strDelim As String
Dim lngCol As Long

strDelim = "||"
strA = Join(Application.Transpose(Range([a1], Cells(Rows.Count, "A").End(xlUp))), strDelim)
strB = Join(Application.Transpose(Range([b1], Cells(Rows.Count, "b").End(xlUp))), strDelim)

For lngCol = Columns("C").Column To Columns("ALC").Column - 2 Step 2
    If Application.CountA(Columns(lngCol)) > 1 Then
    'handle odd column range
        strA = strA & (strDelim & Join(Application.Transpose(Range(Cells(1, lngCol), Cells(Rows.Count, lngCol).End(xlUp))), strDelim))
    Else
    'handle odd column single cell
        If Len(Cells(1, lngCol)) > 0 Then strA = strA & (strDelim & Cells(1, lngCol).Value)
    End If
      If Application.CountA(Columns(lngCol + 1)) > 1 Then
      'handle even column range
    strB = strB & (strDelim & Join(Application.Transpose(Range(Cells(1, lngCol + 1), Cells(Rows.Count, lngCol + 1).End(xlUp))), strDelim))
    Else
     'handle even column single cell
    If Len(Cells(1, lngCol + 1)) > 0 Then strB = strB & (strDelim & Cells(1, lngCol + 1).Value)
    End If
Next

OrigA = Application.Transpose(Split(strA, strDelim))
OrigB = Application.Transpose(Split(strB, strDelim))

[a1].Resize(UBound(OrigA, 1), 1) = OrigA
[b1].Resize(UBound(OrigB, 1), 1) = OrigB

End Sub

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

...