Questions And Answers

More Tutorials

VBA Frequently used string manipulation

String manipulation frequently used examples

Better MID() and other string extraction examples, currently lacking from the web. Please help me make a good example, or complete this one here. Something like this:

DIM strEmpty as String, strNull as String, theText as String
DIM idx as Integer
DIM letterCount as Integer
DIM result as String
strNull = NOTHING
strEmpty = ""
theText = "1234, 78910"
' -----------------
' Extract the word after the comma ", " and before "910" result: "78" ***
' -----------------
' Get index (place) of comma using INSTR
idx = ... ' some explanation here
if idx < ... ' check if no comma found in text
' or get index of comma using FIND
idx = ... ' some explanation here... Note: The difference is...
if idx < ... ' check if no comma found in text
result = MID(theText, ..., LEN(...
' Retrieve remaining word after the comma
result = MID(theText, idx+1, LEN(theText) - idx+1)
' Get word until the comma using LEFT
result = LEFT(theText, idx - 1)
' Get remaining text after the comma-and-space using RIGHT
result = ...
' What happens when things go wrong
result = MID(strNothing, 1, 2) ' this causes ...
result = MID(strEmpty, 1, 2) ' which causes...
result = MID(theText, 30, 2) ' and now...
result = MID(theText, 2, 999) ' no worries...
result = MID(theText, 0, 2)
result = MID(theText, 2, 0)
result = MID(theText -1, 2)
result = MID(theText 2, -1)
idx = INSTR(strNothing, "123")
idx = INSTR(theText, strNothing)
idx = INSTR(theText, strEmpty)
i = LEN(strEmpty)
i = LEN(strNothing) '...

Please feel free to edit this example and make it better. As long as it remains clear, and has in it common usage practices.


In this page (written and validated by ) you learned about VBA Frequently used string manipulation . What's Next? If you are interested in completing VBA tutorial, your next topic will be learning about: VBA Substrings.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.