{"id":12137,"date":"2012-04-24T11:28:24","date_gmt":"2012-04-24T11:28:24","guid":{"rendered":"http:\/\/www.learnexcelmacro.com\/?p=1629"},"modified":"2012-04-24T11:28:24","modified_gmt":"2012-04-24T11:28:24","slug":"vba-programming-string-manipulation","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2012\/04\/vba-programming-string-manipulation\/","title":{"rendered":"VBA Programming : String Manipulation"},"content":{"rendered":"
Many a times, we come across to deal with Strings. Like Length of the String<\/strong>, Concatenating two or more Strings etc. This article teaches you how to deal with strings with the following different operations: <\/p>\n In Excel VBA, two strings can be Joined simply by adding them by using &<\/strong> sign. Refer the below example.<\/p>\n My Name Is Vish<\/strong> This Function is basically used to get the length of the String (Count of All letters including Spaces of a String). Syntax:<\/strong> Example<\/strong><\/p>\n =Len (Vishwa) = 6 and =Len (Learn Excel Macro) = 17<\/p>\n <\/p>\n The Right function extracts the right part of a String. Where: <\/strong> Right(“Learn Excel Macro”, 5) = Macro The Left function extract the left part of a String. <\/p>\n Syntax:<\/strong> Where: <\/strong> Left(“Learn Excel Macro”, 5) = Learn<\/p>\n <\/p>\n The Ltrim function trims the empty spaces of the left portion of the String. <\/p>\n Syntax:<\/strong> Example:<\/strong><\/p>\n Ltrim (” Learn Excel Macro “)= Learn Excel Macro The Rtrim function trims the empty spaces of the right part of the String. Example:<\/strong><\/p>\n Rtrim (” Learn Excel Macro “) = Learn Excel Macro Trim Function<\/strong> is combination of both RTrim and LTrim. It means, it removes all the spaces from Left and Right both the sides. =Trim(String<\/i>)<\/p>\n Example:<\/strong><\/p>\n Trim (\u201c Learn Excel Macro \u201d) = Learn Excel Macro The Mid function extracts a sub-string from the original phrase or string. It takes the following format: String : The Original String Example:<\/strong><\/p>\n Mid(“Learn Excel Macro”, 3, 5) = rn Ex The InStr function looks for a phrase or string that is embedded within the original phrase and returns the starting position of the embedded phrase. Basically this is a search function within a string. Instr (n, String, Search)<\/p>\n Where:<\/strong> Note:<\/strong>It returns the position number where that Search String is found. In the above example, Excel word is found from 7th position of the Original String. That is why it returns 7. The Ucase function<\/strong> converts all the characters of a string to capital letters. On the other hand, the Lcase function <\/strong> converts all the characters of a string to small letters. <\/p>\n Example:<\/strong><\/p>\n Ucase(\u201cLearn Excel Macro\u201d) =LEARN EXCEL MACRO<\/p>\n Lcase(\u201cLearn Excel Macro\u201d) =learn excel macro The Str is the function that converts a number to a string while the Val function converts a string to a number. The two functions are important when we need to perform mathematical operations. The Chr function returns the string that corresponds to an ASCII code while the Asc function converts an ASCII character or symbol to the corresponding ASCII code. There are 255 ASCII codes and as many ASCII characters.<\/p>\n The format of the Chr function is<\/strong><\/p>\n Chr(charcode)<\/p>\n and the format of the Asc function is<\/strong><\/p>\n Asc(Character)<\/p>\n Example:<\/strong><\/p>\n Chr(65)=A <\/p>\n
\n
\n1. Concatenating Strings (Joining Strings)
\n2. Finding Length of a String (Len Function)
\n3. Remove Spaces from a String (Trim Function)
\n4. Left Function
\n5. Right Function
\n6. MID Function
\n7. InStr Function
\n8. Chr Function
\n9. Asc Function
\n<\/strong><\/p>\nConcatenating Strings (Join Strings) :<\/h2>\n
\nPrivate Sub CommandButton1_Click()\nDim Strng1, Strng2, Strng3, RsltStrng As String\nStrng1 = \"My Name \"\nStrng2 = \"Is \"\nStrng3 = \"Vish\"\nRsltStrng = Strng1 & Strng2 & Strng3\nMsgBox (RsltStrng)\nEnd Sub\n<\/code><\/pre>\n
Result:<\/h3>\n
\n<\/p>\nLen Function<\/h2>\n
\nIt returns an Integer Value.<\/p>\n
\n=Len(<\/strong>String<\/i>)<\/strong><\/p>\nRight Function<\/h2>\n
\nSyntax:<\/strong>
\nRight (
\nStr : Is the Original String.
\nn : Number of Character to be extracted from Right of the Original String
\n
\nExample:<\/strong><\/p>\n
\n<\/p>\nLeft Function<\/h2>\n
\nLeft (Str<\/i>, n)<\/p>\n
\nStr : Is the Original String.
\nn : Number of Character to be extracted from Left <\/strong>of the Original String
\n
\nExample:<\/strong><\/p>\nLtrim Function<\/h2>\n
\n=Ltrim(String<\/i>)<\/p>\n
\n<\/p>\nRtrim Function<\/h2>\n
\nSyntax:<\/strong>
\n=Rtrim(String<\/i>)<\/p>\n
\n<\/p>\nTrim function<\/h2>\n
\nSyntax:<\/strong><\/p>\n
\n<\/p>\nMid Function<\/h2>\n
\nSyntax:<\/strong>
\nMid(string, position, n)
\nWhere:<\/strong><\/p>\n
\nPosition : Starting position from where extraction should start
\nn : Number of Characters from the Start position<\/p>\n
\n<\/p>\nInStr function<\/h2>\n
\nSyntax:<\/strong><\/p>\n
\nn : Place from where Search Phrase will be started searching
\nString : Original String where search string will be searched
\nSearch : It is a search String which is searched in the Original String
\n
\nExample:<\/strong>
\nInstr(1, “Learn Excel Macro, “Excel”)=7<\/p>\n
\n<\/p>\nUcase and the Lcase functions<\/h2>\n
\n<\/p>\nStr and Val functions<\/h2>\n
\n<\/p>\nChr and the Asc functions<\/h2>\n
\nChr(122)=z
\nAsc(“B”)=66<\/p>\n