How to split a range of n columns into n parts and copy paste under eachother

MrGambas

New Member
Joined
Jun 24, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Hi,

being a complete newbee in VBA I am struggling with something that is probably very simple.

I have a range that consists of any number of rows, and any number of columns, but not very many. Up to 500 rows and 30 columns. The columns may be grouped by a heading, and a group may be from 2 - 5 columns. It will always be the same number of columns in a group.

What I want to do is to move these groups under each other.

Let's say that the range has 20 rows, and 18 columns. Each group of columns has 3 columns. (Col 1-3 belongs together, 4-6, 7-9 and so on) Then I want col 4-6 to be moved under 1-3, 7-9 under 4-6 and so on.

This is my code so far:

Sub MoveCols()

Dim mR As Range

On Error Resume Next

Set mR = Application.InputBox("Select your Range", , , , , , , 8)
If mR Is Nothing Then MsgBox "Nothing Selected!", vbExclamation: Exit Sub
On Error GoTo 0

Dim HowManyTimes As Integer: HowManyTimes = 1

mR.Copy mR.Offset(mR.Rows.Count).Resize(mR.Rows.Count * HowManyTimes)
End Sub


The ideal solution would be that after I have selected to complete range, from i.e. "A1: R20" I will get a new inputbox where I can enter a number representing the number of columns in each group. When this is done the range is being split by the number in the inputbox, and each group is moved under each other. So if I enter 3 there will be in total 6 groups with 3 columns in each. The range will of course always be split into a number that matches the total number of columns.

Could anyone please point me into the right direction on how to do that?
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Try:
VBA Code:
Sub SplitColumns()
    Application.ScreenUpdating = False
    Dim x As Long, lCol As Long, response As String
    lCol = Cells(1, Columns.Count).End(xlToLeft).Column
    lastrow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    response = InputBox("Please enter the number of columns to group.")
    If response = "" Then Exit Sub
    For x = response + 1 To lCol Step response
        Cells(1, x).Resize(lastrow, response).Copy Cells(Rows.Count, 1).End(xlUp).Offset(1)
    Next x
    Range(Columns(response + 1), Columns(lCol)).Delete
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
That works absolutely perfect! Now I will have a beer and study your code and try to learn something from it.

Thank you very much!!
 
Upvote 0
Just stumbled on this thread and understand that vba was requested but note that with recent functions available in Excel this can also be achieved with a single formula in a single cell.
Small example:

22 06 28.xlsm
ABCDEFGHIJKLMN
1ZECDFHAJXCols/GroupZEC
2APFLRNLQQ3APF
3HRRJSNPGWHRR
4OGIOEKWJYOGI
5HLHSISSORHLH
6QLYOJVQYBQLY
7VBUWADBIHVBU
8XZOKISWOBXZO
9AUVEDRTTEAUV
10DFH
11LRN
12JSN
13OEK
14SIS
15OJV
16WAD
17KIS
18EDR
19AJX
20LQQ
21PGW
22WJY
23SOR
24QYB
25BIH
26WOB
27TTE
28
MAKEARRAY
Cell Formulas
RangeFormula
L1:N27L1=LET(r,A1:I9,rws,ROWS(r),cols,COLUMNS(r),MAKEARRAY(rws*cols/J2,J2,LAMBDA(rw,col,INDEX(r,MOD(rw-1,rws)+1,INT((rw-1)/rws)*J2+col))))
Dynamic array formulas.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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