splitting text/finding data

kroz

Board Regular
Joined
Jan 8, 2009
Messages
53
Hey guys,
I'm kinda new to VBA but i need to implement a search in a text box. I have a form containing a text box in which the user inputs data in the following way:

"owner of the account * NAME (other data)"
"owner of the account * NAME (other data)"
..
(multiple lines - up to 15)
I created a text box to extract the Name from the text with the following code:
Code:
Private Sub ButChange_Click()

Dim Text As String
Dim Line As String
Dim Ntemp As String
Dim Name As String

'Init
Ntemp = ""
Name = ""
Line = ""

'temporary text to be parsed
Text = Chr(13) & TboxII.Text & Chr(13)
'while not temp text is empty
While InStr(Text, Chr(13)) <> 0
  'take first line of text
  Line = Left(Text, InStr(Text, Chr(13)) - 1)
  'selecting the name
  If InStr(Line, "You still have to kill") Then
    Ntemp = Trim(Right(Line, Len(Line) - InStr(Line, "*")))
    Name = Trim(Left(Ntemp, Len(Ntemp) - InStr(Ntemp, "(")))
    ActiveCell.Value = Name
    Cells(ActiveCell.Row + 1, ActiveCell.Column).Select
  End If
Wend
When i run the form my excel crashes. Any idea on what i did wrong?

P.S. A similar post is here - i modified the code a bit: http://www.excelforum.com/excel-programming/674258-combo-list.html#post2052687
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I found the error, but now the code doesn't work as it should:
Rich (BB code):
Private Sub ButChange_Click()

Dim Text As String
Dim Line As String
Dim Ntemp As String
Dim Name As String

'Init
Ntemp = ""
Name = ""
Line = ""
TboxIO.Text = ""

'temporary text to be parsed
Text = Chr(13) & TboxII.Text & Chr(13)
'while not temp text is empty
While InStr(Text, Chr(13)) <> 0
  'take first line
  Line = Left(Text, InStr(Text, Chr(13)) - 1)
  'Name extraction
  If InStr(Line, "Owner of the account ") Then
    Ntemp = Trim(Right(Line, Len(Line) - InStr(Line, "*")))
    ActiveCell.Value = Trim(Left(Ntemp, Len(Ntemp) - InStr(Ntemp, "(")))
    Cells(ActiveCell.Row + 1, ActiveCell.Column).Select
  End If
    'remove the line
    Text = Right(Text, Len(Text) - InStr(Text, Chr(13)))
Wend
I forgot to put the condition to get out of the loop. The problem now is that it doesn't take all the data in the name.
if i input "Owner of the account * John McDee Erby (max access account)"
it will take "John McDee Erby (ma".
In other recordings it didn't take the whole name..

Any idea why?
 
Upvote 0

Forum statistics

Threads
1,214,588
Messages
6,120,412
Members
448,959
Latest member
camelliaCase

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