Change the output of the code

admiral100

Well-known Member
Joined
Jan 17, 2015
Messages
873
Hi ,


This code works fine. (Split Column A & B to 3 columns)

Sub SplitInto3CellsPerColumn()
Dim X As Long, LastRow As Long, vArrIn As Variant, vArrIn2 As Variant, vArrOut As Variant, vArrOut2 As Variant
LastRow = Cells(Rows.Count, "A").End(xlUp).Row

vArrIn = Range("A1:A" & LastRow)
vArrIn2 = Range("B1:B" & LastRow)

ReDim vArrOut(1 To 3, 1 To Int(LastRow / 3) + 1)
ReDim vArrOut2(1 To 3, 1 To Int(LastRow / 3) + 1)

For X = 0 To LastRow - 1
vArrOut(1 + (X Mod 3), 1 + Int(X / 3)) = vArrIn(X + 1, 1)
vArrOut2(1 + (X Mod 3), 1 + Int(X / 3)) = vArrIn2(X + 1, 1)
Next

Range("M1").Resize(3, UBound(vArrOut, 2)) = vArrOut
Range("M6").Resize(3, UBound(vArrOut2, 2)) = vArrOut2


End Sub



The output:

10 40 70
20 50 80
30 60 90


A D G
B E H
C F I


How should I change the code so that the output will be like this: (Odd & Even columns)

10 A 40 D 70 G
20 B 50 E 80 H
30 C 60 F 90 I

Thanks
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Maybe
delete (comment out) the two last code lines
Code:
Range("M1").Resize(3, UBound(vArrOut, 2)) = vArrOut
  Range("M6").Resize(3, UBound(vArrOut2, 2)) = vArrOut2

add these code lines
Code:
Dim varrResult() As Variant, Y As Long, Z As Long
ReDim varrResult(1 To UBound(varrOut, 1), 1 To 2 * UBound(varrOut, 2))

For X = 1 To UBound(varrResult, 1)
    Z = 0
    For Y = 1 To UBound(varrResult, 2) Step 2
        Z = Z + 1
        varrResult(X, Y) = varrOut(X, Z)
    Next Y
    Z = 0
    For Y = 2 To UBound(varrResult, 2) Step 2
        Z = Z + 1
        varrResult(X, Y) = varrOut2(X, Z)
    Next Y
Next X
        
Range("M1").Resize(UBound(varrResult, 1), UBound(varrResult, 2)) = varrResult

Hope this helps

M.
 
Upvote 0
Brilliant! works like a charm.
You are a genius from another league :)


Thanks for your time.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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