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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…