MrExcel Publishing
Your One Stop for Excel Tips & Solutions

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..

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.

Posted by P.P.Ervo on November 06, 2001 7:31 AM

Thank you, that helped me a lot!

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