macro to move columns

fenrost

New Member
Joined
Jul 9, 2010
Messages
6
Hi,
Is there anyone who could help me with one macro? This is the format of the data:

picture1.jpg

And this is what I would like to have at the end:

picture2.jpg


So basically I need a macro that would copy column D&E and put the data below B&C, also copying tasks respectively. and then the same for all other columns. The problem is that the number of columns varies... sometimes it's 30, sometimes it's much more.
 
OK, I copied the data to a new excel spreadsheet and it works. Probably there was something in the old file.

Thanks so much for your help!!!!
 
Upvote 0

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
This may work better but will require you to select the table before running the macro:

Code:
Sub Stack2()
Dim rngMyRng As Range, lngCols As Long, i As Long, j As Long


    
    ' Count number of iterations needed
    lngCols = Application.WorksheetFunction.RoundDown((Selection.Columns.Count - 3) / 2, 0)
    Set rngMyRng = Selection.Cells(2, 1)
    ' Count number of tasks in column A
    j = Range(rngMyRng, rngMyRng.End(xlDown)).Cells.Count
    
    ' Stack columns
    For i = 1 To lngCols
        Range(rngMyRng, rngMyRng.Offset(j - 1, 0)).Copy Destination:=rngMyRng.End(xlDown).Offset(1, 0)
        Range(rngMyRng.Offset(0, 2 * (i - 1) + 3), rngMyRng.Offset(j, 2 * (i - 1) + 4)).Copy Destination:=rngMyRng.Offset(j * i, 1)
    Next i
    
    ' Clear copied cells
    Range(rngMyRng.Offset(-1, 3), rngMyRng.Offset(j - 1, Selection.Columns.Count - 1)).Clear

End Sub
 
Upvote 0
This is even better. And if I also want to delete blank rows at the end of the script, what should I add to the code?
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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