Archive of Mr Excel Message Board


Back to Excel VBA archive index
Back to archive home



How can I get 2 first characters from string?

Posted by P.P.Ervo on November 06, 2001 7:05 AM
Ok, I have the following problem. I'am doing a macro that needs to find out 2 first characters from string. e.g. I have a string which looks like this: "ab_cost"
Now, I don't know how to get those characters "ab". Is there anyone who has any ideas how to complete these problem?
Thanks in advance..

Check out our Excel VBA Resources

Re: How can I get 2 first characters from string?

Posted by Dank on November 06, 2001 7:08 AM

Something like this:-

Sub GetChars()
Dim strAnyString As String
Dim strFirst2Chars As String
strAnyString = "ab_cost"

strFirst2Chars = Left$(strAnyString, 2)

MsgBox strFirst2Chars

End Sub

Hope it helps,
Daniel.


Re: How can I get 2 first characters from string?

Posted by P.P.Ervo on November 06, 2001 7:31 AM
Thank you, that helped me a lot!

Re: How can I get 2 first characters from string?

Posted by Rick on November 07, 2001 5:21 AM
str1 = "ab_cost"
' the mid function starts at the x position (1)
' of the string (str1)
' and passes the y (2) number of characters to the
' receiving string (str2)

str2 = mid(str1, 1, 2)

' str2 now has the first two characters




This archive is from the original message board at www.MrExcel.com.
All contents © 1998-2004 MrExcel.com.
Visit our online store to buy searchable CD's with thousands of VBA and Excel answers.
Microsoft Excel is a registered trademark of the Microsoft Corporation.
MrExcel is a registered trademark of Tickling Keys, Inc.