Parsing a string with numbers, hypens, and commas

Tweeks

Board Regular
Joined
Jul 27, 2004
Messages
52
Hi there,

I have a string that will typically look like:

1,2,5,6,10-14,28

I would like each of the values between the ","'s as a seperate value preferably in an array in VBA.

Is this possible? I've done something simliar in VB a long while ago and its lost on me as to how to do this in VBA if it is at all possible.

Thanks in advance.

edit: Using Excel 97 on a Windows NT system.
 

Some videos you may like

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

bandit_1981

Board Regular
Joined
Aug 17, 2005
Messages
201
Yep use split like the above poster said.

arrString = split(string,",")

acess the values as suchs arrString(0) etc
 

Tweeks

Board Regular
Joined
Jul 27, 2004
Messages
52
Sounds ideal but i dont think excel 97 has split. It doesn't recognise it, 'Complie Error - Sub or Function not defined'

Could i code Split myself?
 

SteveO59L

Well-known Member
Joined
Apr 21, 2004
Messages
7,896

ADVERTISEMENT

Why not use Text to Columns, with the "," as the delimiter ?
 

Tweeks

Board Regular
Joined
Jul 27, 2004
Messages
52
Thats a last option, mainly because when i record doing that switch it specifies how many columns, but i could have from 1 to 30 depending on the string. It would be much much easier to have an array variable that i can loop through.
 

bandit_1981

Board Regular
Joined
Aug 17, 2005
Messages
201
Then i would think you will have to use a loop and go through and stuff each value into an array. THen once you have it filled out redim it using preserve
 

Watch MrExcel Video

Forum statistics

Threads
1,118,289
Messages
5,571,323
Members
412,382
Latest member
Langtn02
Top