Reading string delimited

dave8

Active Member
Joined
Jul 8, 2007
Messages
275
I have a string that looks something like this:

strVar = "string1;string2;string3;string4" but sometimes

strVar = "string1;string2"


The variable, strVar, can have various number of strings, but always delimited by semicolon. I need to create a loop to read each string1, string2... and so forth. How can I split strVar to read each item? I need to somehow find the number of delimited items there are in strVar (sometimes 4 items, sometimes 2 items). How to built a loop for this? Does it have to be put in an array and then read it out?
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
To find the number od strings try this.

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> SplitStrings()<br>    <SPAN style="color:#00007F">Dim</SPAN> strVar <SPAN style="color:#00007F">As</SPAN> String<br>    <SPAN style="color:#00007F">Dim</SPAN> Bits<br>    <br>    <br>    strVar = "string1;string2;string3;string4"<br>    Bits = Split(strVar, ";")<br>    <br>    MsgBox "Number of strings = " & <SPAN style="color:#00007F">UBound</SPAN>(Bits) + 1<br>    <br><SPAN style="color:#007F00">'    Dim i As Long</SPAN><br><SPAN style="color:#007F00">'    For i = 0 To UBound(Bits)</SPAN><br><SPAN style="color:#007F00">'        MsgBox Bits(i)</SPAN><br><SPAN style="color:#007F00">'    Next i</SPAN><br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>

If you want to know what each of the strings is, uncomment the last section.
 
Upvote 0
Study this code, try changing the number of semi-colon separated items assigned to the strVar variable and then look up the Split and Ubound in the VB help files...

Code:
Dim X As Long, strVar, Parts() As String
 
strVar = "string1;string2;string3;string4"

Parts = Split(strVar, ";")
 
' Let's see each individual item in the Parts array
For X = 0 To UBound(Parts)
  MsgBox Parts(X)
Next
 
' You saw them all
MsgBox "No more"
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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