Code to cut all column values & place them under one column

panyagak

Active Member
Joined
Feb 24, 2017
Messages
299
Hi MrExcel

I have a large Excel sheet with common row
headers but with close to 12 columns with
different values. As the row headers are the same,

I need only one column under 1st of the 12 with all column values under it.

Currently, I copy the common rows & columns & paste them downwards 11 times, then cut all values in each column & paste them downwards, again 11 times so that am left with ONLY ONE column for quick sorting. ITS TIRESOME though safer for me.

How can I simplify this with a code?

HELP!!!
 
Joe4.

Next row after Class8.

Sorry, though had mentioned " values in columns M to last" - so I merely restricted myself to M - U. By "last", I meant even up to column Z.

Regards
 
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Let me try my last question again, as this is important and affects how I write the code.
Does your very first row of data start on line one or another row?
 
Upvote 0
Try this:
Code:
Sub MyCopyColumnsData()

    Dim firstRow As Long
    Dim lastRow As Long
    Dim numRows As Long
    Dim counter As Long
    Dim lastCol As Long
    Dim myCol As Long
    
    Application.ScreenUpdating = False
    
'   *****Specify where first row of data starts*****
    firstRow = 2
        
'   Find last row with data in column A
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Calculate the total number of rows
    numRows = lastRow - firstRow + 1
    
'   Find last column with data in row 1
    lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    
'   Set default value of counter
    counter = 1
    
'   Loop through all columns, starting with column L (column 12)
    For myCol = 13 To lastCol
'       Copy columns A-K first
        Range(Cells(firstRow, "A"), Cells(lastRow, "K")).Copy Cells((numRows * counter) + firstRow, "A")
'       Copy next column to column L
        Range(Cells(firstRow, myCol), Cells(lastRow, myCol)).Cut Cells((numRows * counter) + firstRow, "L")
'       Increment counter
        counter = counter + 1
    Next myCol
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Solution
Joe4

Thanks, though late, this code works GREAT & IS ADJUSTABLE (see below) to suit various worksheets.

Regards

Sub MyCopyColumnsData()

Dim firstRow As Long
Dim lastRow As Long
Dim numRows As Long
Dim counter As Long
Dim lastCol As Long
Dim myCol As Long

Application.ScreenUpdating = False

' *****Specify where first row of data starts*****
firstRow = 2

' Find last row with data in column A
lastRow = Cells(Rows.Count, "A").End(xlUp).Row

' Calculate the total number of rows
numRows = lastRow - firstRow + 1

' Find last column with data in row 1
lastCol = Cells(1, Columns.Count).End(xlToLeft).Column

' Set default value of counter
counter = 1

' Loop through all columns, starting with column L (column 12)
For myCol = 23 To lastCol
' Copy columns A-U first
Range(Cells(firstRow, "A"), Cells(lastRow, "U")).Copy Cells((numRows * counter) + firstRow, "A")
' Copy next column to column U
Range(Cells(firstRow, myCol), Cells(lastRow, myCol)).Cut Cells((numRows * counter) + firstRow, "W")
' Increment counter
counter = counter + 1
Next myCol

Application.ScreenUpdating = True

End Sub
 
Upvote 0
You are welcome. Glad it worked out for you.
 
Upvote 0
Sub MyCopyColumnsData()


Dim firstRow As Long
Dim lastRow As Long
Dim numRows As Long
Dim counter As Long
Dim lastCol As Long
Dim myCol As Long

Application.ScreenUpdating = False

' *****Specify where first row of data starts*****
firstRow = 2

' Find last row with data in column A
lastRow = Cells(Rows.Count, "A").End(xlUp).Row

' Calculate the total number of rows
numRows = lastRow - firstRow + 1

' Find last column with data in row 1
lastCol = Cells(1, Columns.Count).End(xlToLeft).Column

' Set default value of counter
counter = 1

' Loop through all columns, starting with column Y (column 25)
For myCol = 26 To lastCol
' Copy columns A-K first
Range(Cells(firstRow, "A"), Cells(lastRow, "X")).Copy Cells((numRows * counter) + firstRow, "A")
' Copy next column to column L
Range(Cells(firstRow, myCol), Cells(lastRow, myCol)).Cut Cells((numRows * counter) + firstRow, "Y")
' Increment counter
counter = counter + 1
Next myCol

Application.ScreenUpdating = True

End Sub
 
Upvote 0
Joe4

Please help here.

Am trying to run this code on this SAMPLE data without success:
Data A to Z to copy/paste downwards & values Z to AG to cut/paste downwards under column Y.

INDI/(BD2/CF)/BD4/TD3/TD2/TD1ZAAABACADAEAFAG
12.00222851.200.120.01120.021,200.2212,002.23120,022.281,200,222.85
32.84533773.280.330.03328.453,284.5332,845.34328,453.383,284,533.77
6.64362890.660.070.0166.44664.366,643.6366,436.29664,362.89
59.33763785.930.590.06593.385,933.7659,337.64593,376.385,933,763.78
0.75433080.080.010.007.5475.43754.337,543.3175,433.08
31.29624553.130.310.03312.963,129.6231,296.25312,962.463,129,624.55
0.29377320.030.000.002.9429.38293.772,937.7329,377.32
80.36037478.040.800.08803.608,036.0480,360.37803,603.758,036,037.47
0.59463590.060.010.005.9559.46594.645,946.3659,463.59
0.92349620.090.010.009.2392.35923.509,234.9692,349.62

<colgroup><col><col span="6"><col><col></colgroup><tbody>
</tbody>

Sub MyCopyColumnsData()


Dim firstRow As Long
Dim lastRow As Long
Dim numRows As Long
Dim counter As Long
Dim lastCol As Long
Dim myCol As Long

Application.ScreenUpdating = False

' *****Specify where first row of data starts*****
firstRow = 2

' Find last row with data in column A
lastRow = Cells(Rows.Count, "A").End(xlUp).Row

' Calculate the total number of rows
numRows = lastRow - firstRow + 1

' Find last column with data in row 1
lastCol = Cells(1, Columns.Count).End(xlToLeft).Column

' Set default value of counter
counter = 1

' Loop through all columns, starting with column Y (column 25)
For myCol = 26 To lastCol
' Copy columns A-K first
Range(Cells(firstRow, "A"), Cells(lastRow, "X")).Copy Cells((numRows * counter) + firstRow, "A")
' Copy next column to column Y
Range(Cells(firstRow, myCol), Cells(lastRow, myCol)).Cut Cells((numRows * counter) + firstRow, "Y")
' Increment counter
counter = counter + 1
Next myCol

Application.ScreenUpdating = True

End Sub


Regards
Patrick
 
Upvote 0
Sorry:

Data A to X to copy/paste downwards & values Z to AG to cut/paste downwards under column Y.
 
Upvote 0

Forum statistics

Threads
1,215,159
Messages
6,123,351
Members
449,097
Latest member
thnirmitha

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