Copy Values from Multiple Columns to 2 Columns

KH4W4R

New Member
Joined
May 26, 2015
Messages
7
Hello All,

Firstly I would like to apologise I have asked same question on another excel forum, I am after a quick solution not trying to disrespect rules. Problem I am facing is I am using following code to copy values from multiple columns to one column.

Following code works perfect for me; No I want this code to copy values of two columns and paste them in column A:B.

Raw Data
DayCountDayCountDayCount
Monday522Monday365Monday540
Tuesday656Tuesday458Tuesday65
Wednesday699Wednesday661Wednesday23
Thursday120Thursday874Thursday1051
Friday985Friday321Friday75
Saturday220Saturday987Saturday66
Sunday654Sunday200Sunday24

<tbody>
</tbody>

Results I am after;
DayCount
Monday522
Tuesday656
Wednesday699
Thursday120
Friday985
Saturday220
Sunday654
DayCount
Monday365
Tuesday458
Wednesday661
Thursday874
Friday321
Saturday987
Sunday200
DayCount
Monday540
Tuesday65
Wednesday23
Thursday1051
Friday75
Saturday66
Sunday24

<tbody>
</tbody>


Code:
[COLOR=#333333]Sub test()    Dim lastCol As Long, lastRowA As Long, lastRow As Long, i As Long[/COLOR]


    'find last non empty column number'
    lastCol = Cells(1, Columns.Count).End(xlToLeft).Column


    'loop through all columns, starting from column B'
    For i = 2 To lastCol
        'find last non empty row number in column A'
        lastRowA = Cells(Rows.Count, "A").End(xlUp).Row
        'find last non empty row number in another column'
        lastRow = Cells(Rows.Count, i).End(xlUp).Row


        'copy data from another column'
        Range(Cells(1, i), Cells(lastRow, i)).Copy
        'paste data to column A'
        Range("A" & lastRowA + 1).PasteSpecial xlPasteValues


        'Clear content from another column. if you don't want to clear content from column, remove next line'
        Range(Cells(1, i), Cells(lastRow, i)).ClearContents
    Next i


    Application.CutCopyMode = False [COLOR=#333333]End Sub[/COLOR]



 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG06Oct59
[COLOR="Navy"]Dim[/COLOR] oRay [COLOR="Navy"]As[/COLOR] Variant, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Rw [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Ray [COLOR="Navy"]As[/COLOR] Variant
oRay = ActiveSheet.Range("A1").CurrentRegion
ReDim Ray(1 To UBound(oRay, 1) * UBound(oRay, 2), 1 To 2)
[COLOR="Navy"]For[/COLOR] Ac = 1 To UBound(oRay, 2) [COLOR="Navy"]Step[/COLOR] 2
    [COLOR="Navy"]For[/COLOR] Rw = 1 To UBound(oRay, 1)
        c = c + 1
        Ray(c, 1) = oRay(Rw, Ac)
        Ray(c, 2) = oRay(Rw, Ac + 1)
    [COLOR="Navy"]Next[/COLOR] Rw
[COLOR="Navy"]Next[/COLOR] Ac
ActiveSheet.Range("A1").CurrentRegion.ClearContents
Range("A1").Resize(c, 2) = Ray
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Code:
Sub test()

Dim lastCol As Long, lastRowA As Long, lastRow As Long, i As Long


    'find last non empty column number'
    lastCol = Cells(1, Columns.Count).End(xlToLeft).Column


    'loop through all columns, starting from column B'
    For i = 2 To lastCol / 2
        'find last non empty row number in column A'
        lastRowA = Cells(Rows.Count, "A").End(xlUp).Row
        'find last non empty row number in another column'
        lastRow = Cells(Rows.Count, 2 * i - 1).End(xlUp).Row
        If Cells(Rows.Count, 2 * i).End(xlUp).Row > lastRow Then
        lastRow = Cells(Rows.Count, 2 * i).End(xlUp).Row
        End If
        

        'copy data from another column'
        Range(Cells(1, 2 * i - 1), Cells(lastRow, 2 * i)).Copy
        'paste data to column A'
        Range("A" & lastRowA + 1).PasteSpecial xlPasteValues


        'Clear content from another column. if you don't want to clear content from column, remove next line'
        Range(Cells(1, 2 * i - 1), Cells(lastRow, 2 * i)).ClearContents
    Next i


    Application.CutCopyMode = False
    End Sub
 
Upvote 0

Forum statistics

Threads
1,215,519
Messages
6,125,297
Members
449,218
Latest member
Excel Master

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top