splitting cell data into columns

Jon von der Heyden

MrExcel MVP, Moderator
Joined
Apr 6, 2004
Messages
10,907
Office Version
  1. 365
Platform
  1. Windows
Hi,

This is a piece of a table that I'm working with:
International Repricing Model.xls
BCDE
2CountryOperative DateCodeRange
3Afghanistan18.03.20059370 - 72, 79
4Albania18.10.200235568, 69
5Algeria23.07.200421361, 62
6Algeria01.05.20022137
7Andorra01.05.20023763
8Andorra21.05.20043764
9Andorra21.05.20043766
10Angola17.10.200324491
11Angola17.10.200324492
12Antigua & Barbuda01.01.20041268464
13Antigua & Barbuda01.01.20041268723-729
14Antigua & Barbuda01.01.20041268764
15Antigua & Barbuda01.01.20041268770-775
16Antilles (Netherlands)17.06.20055993181, 3184
17Antilles (Netherlands)15.04.20055994161, 4165, 4166
18Antilles (Netherlands)15.04.2005599520 - 524, 526
19Antilles (Netherlands)15.04.200559955
20Antilles (Netherlands)29.10.2004599580, 581
21Antilles (Netherlands)15.04.200559978, 79
22Antilles (Netherlands)15.04.2005599951, 952, 954, 956
Sheet2


I'm trying to split the range into all their various elements into different columns. Num - Num depicts a range of prefixes between the numbers, those split out by "," are individual prefixes.

Any idea how I can create a series of columns displaying all prefixes? I have thousands...

Many thanks,
Jon
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi Jon,

How about if you select column E & use Data / Text to columns, specifying comma as your delimiter?
 
Upvote 0
Hi,

This macro will split the ranges into columns E onwards:
Code:
Sub SplitRanges()
Dim iCol As Integer, iPtr As Integer, iRangePtr As Integer
Dim lRange(1 To 2) As Long
Dim WS As Worksheet
Dim R As Range
Dim vCur As Variant
Dim sChar As String, sElement As String

Set WS = Sheets("Sheet1")
For Each R In WS.Range("D2:D" & WS.Cells(Rows.Count, 4).End(xlUp).Row)
    iCol = 0
    vCur = WorksheetFunction.Substitute(R.Value, " ", "")
    sElement = ""
    iRangePtr = 1
    lRange(1) = 0
    lRange(2) = 0
    For iPtr = 1 To Len(vCur)
        sChar = Mid$(vCur, iPtr, 1)
        Select Case sChar
        Case "-"
            If iRangePtr< 2 Then
                iRangePtr = iRangePtr + 1
                lRange(2) = 0
            End If
        Case ","
            iRangePtr = 1
            Do
                iCol = iCol + 1
                R.Offset(0, iCol).Value = lRange(1)
                lRange(1) = lRange(1) + 1
            Loop Until lRange(1) > lRange(2)
            lRange(1) = 0
            lRange(2) = 0
        Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
            lRange(iRangePtr) = lRange(iRangePtr) * 10 + sChar
            If iRangePtr = 1 Then lRange(2) = lRange(1)
        End Select
    Next iPtr
    Do
        iCol = iCol + 1
        R.Offset(0, iCol).Value = lRange(1)
        lRange(1) = lRange(1) + 1
    Loop Until lRange(1) > lRange(2)
Next R

End Sub
noj.xls
ABCDEFGHIJK
1CountryOperative DateCodeRange
2Afghanistan18.03.20059370 - 72, 7970717279
3Albania18.10.200235568, 696869
4Algeria23.07.200421361, 626162
5Algeria01.05.200221377
6Andorra01.05.200237633
7Andorra21.05.200437644
8Andorra21.05.200437666
9Angola17.10.20032449191
10Angola17.10.20032449292
11Antigua & Barbuda01.01.20041268464464
12Antigua & Barbuda01.01.20041268723-729723724725726727728729
13Antigua & Barbuda01.01.20041268764764
14Antigua & Barbuda01.01.20041268770-775770771772773774775
15Antilles (Netherlands)17.06.20055993181, 318431813184
16Antilles (Netherlands)15.04.20055994161, 4165, 4166416141654166
17Antilles (Netherlands)15.04.2005599520 - 524, 526520521522523524526
18Antilles (Netherlands)15.04.20055995555
19Antilles (Netherlands)29.10.2004599580, 581580581
20Antilles (Netherlands)15.04.200559978, 797879
21Antilles (Netherlands)15.04.2005599951, 952, 954, 956951952954956
Sheet1
 
Upvote 0
Can you do a reverse of this? Taking the array and putting it back into a list seperated by dashes and commas?
 
Upvote 0

Forum statistics

Threads
1,215,103
Messages
6,123,106
Members
449,096
Latest member
provoking

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