Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default How to read part of a text string only?

I have a string "xxxx_yy_zzzzzz". I would like to store "xxxx" as a string in
a variable, "yy" as a string in another variable, and "zzzzzz" as a string in
another variable. Is there code in visual basic for excel that would help me
achieve this?

Thanks

Ahsan
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default How to read part of a text string only?

Hi Ahsan

Is it always 4_2_6 characters
Is there always a underscore between the text

--
Regards Ron de Bruin
http://www.rondebruin.nl



"Ahsan" wrote in message ...
I have a string "xxxx_yy_zzzzzz". I would like to store "xxxx" as a string in
a variable, "yy" as a string in another variable, and "zzzzzz" as a string in
another variable. Is there code in visual basic for excel that would help me
achieve this?

Thanks

Ahsan



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default How to read part of a text string only?

Is it always going to be the same number of characters? You could use the
left$, mid$, and right$ functions.
varstring = "xxxx_yy_zzzzzz"
varx = left$(varstring,4)
vary = mid$(varstring,6,2)
varz = right$(varstring,6)

Or you can use the split command. This will split the string based on a
delimiter and store the values in an array.
vararray = split(varstring, "_")

Brian


"Ahsan" wrote in message
...
I have a string "xxxx_yy_zzzzzz". I would like to store "xxxx" as a string

in
a variable, "yy" as a string in another variable, and "zzzzzz" as a string

in
another variable. Is there code in visual basic for excel that would help

me
achieve this?

Thanks

Ahsan



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default How to read part of a text string only?

As a side note, it doesn't have to be the same number of characters to use
the left, right, and mid functions, but you would have to do a bit more
coding to find the location of the "_". The "instr" command will give you
the position of the "_" character.


"Brian K. Sheperd" wrote in message
...
Is it always going to be the same number of characters? You could use the
left$, mid$, and right$ functions.
varstring = "xxxx_yy_zzzzzz"
varx = left$(varstring,4)
vary = mid$(varstring,6,2)
varz = right$(varstring,6)

Or you can use the split command. This will split the string based on a
delimiter and store the values in an array.
vararray = split(varstring, "_")

Brian


"Ahsan" wrote in message
...
I have a string "xxxx_yy_zzzzzz". I would like to store "xxxx" as a

string
in
a variable, "yy" as a string in another variable, and "zzzzzz" as a

string
in
another variable. Is there code in visual basic for excel that would

help
me
achieve this?

Thanks

Ahsan





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 71
Default How to read part of a text string only?

Hello,

Assuming:

1) _ is a delimeter
2) You're using Excel 2000+

You could use the Split function to create an array of three elements from
your example. E.g.,

Sub yadda()
Dim strArr() As String, i As Long
Const myStr As String = "xxxx_yy_zzzzzz"
Let strArr = Split(myStr, "_")
For i = LBound(strArr) To UBound(strArr)
Debug.Print strArr(i),
Next
End Sub

Regards,
Nate Oliver



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting right part of text string Sue Compelling Excel Worksheet Functions 5 November 5th 09 07:54 PM
Delete part of a text string IanP Excel Worksheet Functions 3 April 6th 09 12:34 PM
Extract part of a text string Martin B Excel Worksheet Functions 7 January 13th 08 04:36 PM
Omitting right part of text string klysell Excel Worksheet Functions 6 January 13th 08 12:59 PM
How do I extract part of a text string Brennan Excel Discussion (Misc queries) 2 November 28th 06 07:26 PM


All times are GMT +1. The time now is 06:33 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"