Formula or VBA to split cell data into 5 columns

mcgee1505

New Member
Joined
May 20, 2020
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have searched the forum for a solution but nothing seems to fit what I need.

I have a worksheet with thousands of rows of data covering columns A - P. In column A is a product code set out in 5 sections with a 'dash' seperator e.g. 123456-ABC-ABC-ABC-123456. I would like a formula or VBA code to take the product code in column A and split it across columns Q - U. Can anyone help please?

Thanks in advance.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
you can use excel's built-in function text-to-column with "-" as delimiter
 
Upvote 0
May be
VBA Code:
Sub test()
    Dim i
    For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
        Range("Q" & i).Resize(, 5) = Split(Cells(i, 1), "-")
    Next
End Sub
 
Upvote 0
Thanks AlanY but I tried that and the last section number didn't come across the same e.g if it is 000001 it transferes as 1, I need the number to be the same.

Thanks mohadin, this works good but it is very slow at running the code. I takes approx 2 seconds a row and there are thousands of rows, is there a way to make it run faster?
 
Upvote 0
Try
VBA Code:
Sub test()
    Dim i, ii, a
    a = Cells(2, 1).Resize(Cells(Rows.Count, 1).End(xlUp).Row - 1)
    ReDim b(1 To UBound(a), 1 To 5)
    For i = 1 To UBound(a)
        For ii = 1 To 5
            b(i, ii) = Split(a(i, 1), "-")(ii - 1)
        Next
    Next
    Range("Q2").Resize(UBound(b), UBound(b, 2)) = b
End Sub
 
Upvote 0
Thanks mohadin, this was very fast but same as the excel function above, leaves the last number as a single digit but it needs to remain a six digit number, I need the number to be the same?
 
Upvote 0
1607950248228.png
 
Upvote 0
You can use text to columns, but on page 3 of the wizard in the data preview section select the last column & then click text under the data format.
 
Upvote 0
Solution
I have even formatted the columns to 'General' but it still doesn't work?

Number
123456-ABC-ACR-EMF-000001Data deleted for confidentiality123456ABCACREMF1
123456-ABC-ACR-EMF-000002123456ABCACREMF2
123456-ABC-ACR-EMF-000003123456ABCACREMF3
123456-ABC-ACR-EMF-000004123456ABCACREMF4
123456-ABC-ACR-EMF-000005123456ABCACREMF5
123777-ABC-ACR-EMF-000154123777ABCACREMF154
123777-ABC-ACR-EMF-000155123777ABCACREMF155
123777-ABC-ACR-EMF-000156123777ABCACREMF156
123777-ABC-ACR-EMF-000157123777ABCACREMF157
123777-ABC-ACR-EMF-000158123777ABCACREMF158
123777-ABC-ACR-EMF-000159123777ABCACREMF159
123777-ABC-ACR-EMF-000160123777ABCACREMF160
435666-ABC-GGG-EMF-000352435666ABCGGGEMF352
435666-ABC-GGG-EMF-000353435666ABCGGGEMF353
435666-ABC-GGG-EMF-000354435666ABCGGGEMF354
435666-ABC-GGG-EMF-000355435666ABCGGGEMF355
435666-ABC-GGG-EMF-000356435666ABCGGGEMF356
435666-ABC-GGG-EMF-000357435666ABCGGGEMF357
123489-ABC-TBT-SSS-000002123489ABCTBTSSS2
123489-ABC-TBT-SSS-000003123489ABCTBTSSS3
123489-ABC-TBT-SSS-000004123489ABCTBTSSS4
123489-ABC-TBT-SSS-000005123489ABCTBTSSS5
123489-ABC-TBT-SSS-000006123489ABCTBTSSS6
123489-ABC-TBT-SSS-000007123489ABCTBTSSS7
123489-ABC-TBT-SSS-000008123489ABCTBTSSS8
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

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