Copy every 4 cells and paste it into in the next row till there is a empty cell

sairam89

New Member
Joined
Jun 13, 2018
Messages
4
Hi
I am a newbie to macros and i want to use macro to copy and paste the table data

Please look into the below tables and you will have a clear picture of what kind of result i am looking for

Input

User
ODMTODMTODMT
A
101106111062012220110111
B
201
2032220310611

<colgroup><col style="width:48pt" width="64" span="13"> </colgroup><tbody>
</tbody>

Output

ODMT
A10110611
10620122
20110111
B20120322
20310611

<colgroup><col style="width:48pt" width="64" span="5"> </colgroup><tbody>
</tbody>
Thanks in advance
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Welcome to the MrExcel board!

Try this in a copy of your workbook.

Code:
Sub Rearrange()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, r As Long, uba2 As Long
  
  a = Range("A1").CurrentRegion.Value
  uba2 = UBound(a, 2)
  ReDim b(1 To (uba2 - 1) / 4 * (UBound(a, 1) - 1), 1 To 5)
  For i = 2 To UBound(a)
    b(r + 1, 1) = a(i, 1)
    For j = 2 To uba2 Step 4
      r = r + 1
      For k = 1 To 4
        b(r, k + 1) = a(i, j + k - 1)
      Next k
    Next j
  Next i
  With Range("A" & Rows.Count).End(xlUp).Offset(2).Resize(, 5)
    .Value = Range("A1").Resize(, 5).Value
    .Offset(1).Resize(UBound(b)).Value = b
  End With
End Sub

My data & results:


Book1
ABCDEFGHIJKLM
1UserODMTODMTODMT
2A101106111062012220110111
3B2012032220310611
4
5UserODMT
6A10110611
710620122
820110111
9B20120322
1020310611
Rearrange
 
Upvote 0
How can I skip blank rows in this table while rearranging
See if this is it. (Red = deletions, blue = additions)
Rich (BB code):
Sub Rearrange_v2()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, r As Long, uba2 As Long
  
 <del> a = Range("A1").CurrentRegion.Value</del>
  a = Range("A1", Range("A" & Rows.Count).End(xlUp)).Resize(, Cells(1, Columns.Count).End(xlToLeft).Column).Value
  uba2 = UBound(a, 2)
  ReDim b(1 To (uba2 - 1) / 4 * (UBound(a, 1) - 1), 1 To 5)
  For i = 2 To UBound(a)
    If a(i, 1) <> "" Then
      b(r + 1, 1) = a(i, 1)
      For j = 2 To uba2 Step 4
        r = r + 1
        For k = 1 To 4
          b(r, k + 1) = a(i, j + k - 1)
        Next k
      Next j
    End If
  Next i
  With Range("A" & Rows.Count).End(xlUp).Offset(2).Resize(, 5)
    .Value = Range("A1").Resize(, 5).Value
    .Offset(1).Resize(UBound(b)).Value = b
  End With
End Sub
 
Upvote 0
Blank rows are still there problem is not resolved

Input

ODMTODMTODMT
S283012302812302812
B283012
C283012302812302812
V283012302812
D283012302812302812

<colgroup><col width="64" span="13" style="width:48pt"> </colgroup><tbody>
</tbody>

Output

ODMT
S283012
302812
302812
B283012
C283012
302812
302812
V283012
302812
D283012
302812
302812

<colgroup><col width="64" span="5" style="width:48pt"> </colgroup><tbody>
</tbody>

<colgroup><col width="64" span="5" style="width:48pt"> </colgroup><tbody>
</tbody>
 
Upvote 0
Blank rows are still there problem is not resolved
Sorry, you didn't previously specify which table to skip blank rows in and I guessed the original table, not the result table. :)

Try this (which should skip blanks in either table based on your sample data)

Code:
Sub Rearrange_v3()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, r As Long, uba2 As Long
  
  a = Range("A1", Range("A" & Rows.Count).End(xlUp)).Resize(, Cells(1, Columns.Count).End(xlToLeft).Column).Value
  uba2 = UBound(a, 2)
  ReDim b(1 To (uba2 - 1) / 4 * (UBound(a, 1) - 1), 1 To 5)
  For i = 2 To UBound(a)
    If a(i, 1) <> "" Then
      b(r + 1, 1) = a(i, 1)
      For j = 2 To uba2 Step 4
        If a(i, j) = "" Then Exit For
        r = r + 1
        For k = 1 To 4
          b(r, k + 1) = a(i, j + k - 1)
        Next k
      Next j
    End If
  Next i
  With Range("A" & Rows.Count).End(xlUp).Offset(2).Resize(, 5)
    .Value = Range("A1").Resize(, 5).Value
    .Offset(1).Resize(UBound(b)).Value = b
  End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,564
Messages
6,125,575
Members
449,237
Latest member
Chase S

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