Extracting Data from long string

Dave Punky

Board Regular
Joined
Jan 7, 2010
Messages
133
Hi all,

I have an idea how to do this, but I've not been having much luck.

Basically what I have is a long string with each section split by a tab. Here is some sample data to explain what I mean:

Code:
Ref:               2Ref:             Code:             Date:            Address:

1000000,           21111111          0123,456789,     01/09/2011,       MR X., Address Line 1, Address Line 2, Zip Code
1000001,           21116111          0123,785412,     01/09/2011,       MR Y., Address Line 1, Address Line 2, Zip Code
1000002,           21221452          0123,874114,     01/09/2011,       MR Z., Address Line 1, Address Line 2, Zip Code

Basically what I need to do is extract each 3rd section (code section) from each line and take that data, then loop and take the 3rd section from the next line down.

I don't want to put the data into a worksheet (which is why I'm not using Excels import data function) as I'm then going to take that data and complete an action, then loop it and start again.

So using the data above, I basically want to extract the data like so:
Code:
Dim refcode as string
Dim alldata as variant

alldata = split(Table Of Data, vbTab)

Do while X <= Last Array Entry in alldata
     refcode = 0123,456789, (then 0123,785412, and then 0123,874114,)
     call action(refcode)

X = X+1
Loop

I was thinking of using Split to split the whole string (as each section being split by a tab I can set this as the split condition) but the problem I have is trying to work a loop to cycle through the whole split variant, and how to determine when the end of the loop is also.

The end of each line is also separated by an enter character so I'm not sure if it would be better to split each line first and then take the data or just by splitting by tabs?

I'm not sure if I've explained this very well, but hopefully somebody can point me in the right direction!
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Example: This splits by Lines and then by entries in each line.

<font face=Courier New>    <SPAN style="color:#00007F">Dim</SPAN> vLines <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>, vEntries <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br>        <br>    <SPAN style="color:#007F00">'Split data by lines</SPAN><br>    vLines = Split(Table_Of_Data, vbCrLf)<br><br>    <SPAN style="color:#00007F">For</SPAN> i = <SPAN style="color:#00007F">LBound</SPAN>(vLines) <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(vLines)<br>        <SPAN style="color:#007F00">'Split lines by entries</SPAN><br>        vEntries = Split(vLines(i), vbTab)<br>        <SPAN style="color:#007F00">'Test third entry (base zero so third entry is index #2)</SPAN><br>        <SPAN style="color:#00007F">If</SPAN> vEntries(2) = "0123,785412," <SPAN style="color:#00007F">Or</SPAN> vEntries(2) = "0123,874114," <SPAN style="color:#00007F">Then</SPAN><br>            <SPAN style="color:#00007F">Call</SPAN> action(vEntries(2))<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> i</FONT>
 
Last edited:
Upvote 0
I'm not entirely clear on your "long string" of data. Is the below quoted data all in a single non-array String variable?

Basically what I have is a long string with each section split by a tab. Here is some sample data to explain what I mean:

Code:
Ref:               2Ref:             Code:             Date:            Address:
 
1000000,           21111111          0123,456789,     01/09/2011,       MR X., Address Line 1, Address Line 2, Zip Code
1000001,           21116111          0123,785412,     01/09/2011,       MR Y., Address Line 1, Address Line 2, Zip Code
1000002,           21221452          0123,874114,     01/09/2011,       MR Z., Address Line 1, Address Line 2, Zip Code
If so, can we assume the separate lines of text that you show are separated by Line Feed characters or, if you read it in from a text file, Windows NewLine character sequences (Carriage Return followed by a Line Feed)? If so, which one? If not to either of the above, then please explain where the text is and how it got there.
 
Last edited:
Upvote 0
Thanks for your replies!

@ Rick

The data is copied into clipboard from a .txt file and then pasted into a UserForm textbox on my sheet which supports multiline. So it's essentially one single string of text when it is pasted into the userform. It is a standard return/enter to break each line, so I guess this is classed as Windows NewLine?
 
Upvote 0
Hi all,

Ok so maybe I'm going completely wrong here but I keep getting a subscript error when trying to use the method AlphaFrog has suggested.

Here's exactly how I've coded it:
Code:
Private Sub CommandButton1_Click()
Dim vLines As Variant, vEntries As Variant
Dim ref1 As String, ref2 As String
Dim newliner As Integer
Dim i As Integer
newliner = 1
 
'Split data by lines
vLines = Split(Me.tbwheretextis.value, vbCrLf)
For i = LBound(vLines) To UBound(vLines)
'Split lines by entries
vEntries = Split(vLines(i), vbTab)
'Test third entry (base zero so third entry is index #2)
ref1 = CStr(vEntries(2))
ref2 = CStr(vEntries(3))
 
Sheets("test").Range("A" & newliner).Value = ref1
Sheets("test").Range("B" & newliner).Value = ref2
newliner = newliner + 1
 
Next i
 
End Sub

I'm not really sure where it's going wrong...
 
Upvote 0
<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CommandButton1_Click()<br>    <SPAN style="color:#00007F">Dim</SPAN> vLines <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>, vEntries <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> i      <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <br>    <SPAN style="color:#00007F">With</SPAN> Sheets("Test")<br>        <SPAN style="color:#007F00">'Split data by lines</SPAN><br>        vLines = Split(Me.tbwheretextis.Value, vbCrLf)<br>        <SPAN style="color:#00007F">For</SPAN> i = <SPAN style="color:#00007F">LBound</SPAN>(vLines) <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(vLines)<br>            <SPAN style="color:#007F00">'Split lines by entries</SPAN><br>            vEntries = Split(vLines(i), vbTab)<br>            <br>            <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(vEntries) >= 2 <SPAN style="color:#00007F">Then</SPAN><br>                .Range("A" & i + 1).Value = <SPAN style="color:#00007F">CStr</SPAN>(vEntries(2))<br>            <SPAN style="color:#00007F">Else</SPAN><br>                .Range("A" & i + 1).Value = "N/A"<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>            <br>            <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(vEntries) >= 3 <SPAN style="color:#00007F">Then</SPAN><br>                .Range("B" & i + 1).Value = <SPAN style="color:#00007F">CStr</SPAN>(vEntries(3))<br>            <SPAN style="color:#00007F">Else</SPAN><br>                .Range("B" & i + 1).Value = "N/A"<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> i<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,595
Members
452,927
Latest member
whitfieldcraig

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