Split data

Deepk

Board Regular
Joined
Mar 21, 2018
Messages
105
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I want a macro for changing the following data

Input:
AB2345
SD7890
AB
XC
CB2345
GH6543
SD
FG
ER2345
CV3455
GH
MN
WX9876
RE3456
HJ
KL

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

Output:

AB2345AB
SD7890XC
CB2345SD
GH6543FG
ER2345GH
CV3455MN
WX9876HJ
RE3456KL

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

Thanks in advance.
 
Last edited:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi
sorry deleted for miss understanding
 
Last edited:
Upvote 0
Is this what you want?

Your ata in a1:b4
Code:
Sub test()
    For i = 1 To 4
        For j = 1 To 2
            a = Split(Cells(i, j), Chr(10))
            Cells(i, j).Offset(i - 1, 3) = a(0)
            Cells(i, j).Offset(i, 3) = a(1)
        Next
    Next
End Sub
 
Upvote 0
Is this what you want?

Your ata in a1:b4
Code:
Sub test()
    For i = 1 To 4
        For j = 1 To 2
            a = Split(Cells(i, j), Chr(10))
            Cells(i, j).Offset(i - 1, 3) = a(0)
            Cells(i, j).Offset(i, 3) = a(1)
        Next
    Next
End Sub

Excellento..

Thank you very much.
 
Upvote 0
How big (number of rows) is your actual data?
 
Upvote 0
hi Peter,

Three to four thousand rows.

Regards
OK thanks. That is actually pretty small for vba so the fact that this is roughly 30 times faster probably wouldn't be noticeable. :)
If there was a lot more data this would begin to show up as noticeably faster.

Code:
Sub Split_Data()
  Dim a As Variant, b As Variant, c As Variant
  Dim i As Long
  
  a = Range("A1", Range("B" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a) * 2, 1 To 2)
  For i = 1 To UBound(a)
    c = Split(a(i, 1), Chr(10))
    b(i * 2 - 1, 1) = c(0)
    b(i * 2, 1) = c(1)
    c = Split(a(i, 2), Chr(10))
    b(i * 2 - 1, 2) = c(0)
    b(i * 2, 2) = c(1)
  Next
  Range("D1:E1").Resize(UBound(b)).Value = b
End Sub
 
Upvote 0
OK thanks. That is actually pretty small for vba so the fact that this is roughly 30 times faster probably wouldn't be noticeable. :)
If there was a lot more data this would begin to show up as noticeably faster.

Thank you Peter. Its working. :)
 
Upvote 0
Thank you Peter. Its working. :)
You're welcome.
Just repeating that for 3 or 4 thousand rows it wouldn't matter which of the suggested codes you use - whatever you feel most comfortable with.
If you had 200,000 rows then I would definitely use the code I suggested. :)
 
Upvote 0
Here is one more macro to consider (what is interesting about this one is it does not use a loop)...
Code:
Sub Split_Data()
  Dim LastRow As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Range("C1").Resize(2 * LastRow) = Application.Transpose(Split(Join(Application.Transpose(Range("A1:A" & LastRow)), vbLf), vbLf))
  Range("D1").Resize(2 * LastRow) = Application.Transpose(Split(Join(Application.Transpose(Range("B1:B" & LastRow)), vbLf), vbLf))
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,101
Messages
6,128,842
Members
449,471
Latest member
lachbee

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