ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to read part of a text string only? (https://www.excelbanter.com/excel-programming/325450-how-read-part-text-string-only.html)

Ahsan

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

Ron de Bruin

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




Brian K. Sheperd

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




Nate Oliver[_3_]

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


Brian K. Sheperd

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







All times are GMT +1. The time now is 12:12 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com