0% found this document useful (0 votes)
75 views

VBA - String Parsing. String Parsing Involves Looking Through - by Breakcorporate - Medium

The document summarizes how to parse and manipulate strings in VBA. It provides an example of splitting a string on spaces into an array of words. It then loops through each word to count the number of "t" characters and adds words with more than one "t" to a separate array. This demonstrates how to identify parts of a string through parsing and manipulation.

Uploaded by

vaskore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

VBA - String Parsing. String Parsing Involves Looking Through - by Breakcorporate - Medium

The document summarizes how to parse and manipulate strings in VBA. It provides an example of splitting a string on spaces into an array of words. It then loops through each word to count the number of "t" characters and adds words with more than one "t" to a separate array. This demonstrates how to identify parts of a string through parsing and manipulation.

Uploaded by

vaskore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

breakCorporate 6 Followers About Follow Upgrade

VBA — String Parsing


breakCorporate Jul 10, 2016 · 2 min read

String parsing involves looking through each character in a string. Typically


it is best to split the string on spaces or another indicator such as “.” or “/” so
only individual words are being observed. This process can used to
manipulate words in a string or identify parts of strings.

For Example:

Dim checkString as String


Dim stringArray() as String
Dim tCount as Integer
Dim tWordArray() as String

checkString = "Trying to identify words that have more than one t:


like matter or scatter"

stringArray = Split(checkString, " ")

For each word in checkString

tCount = 0

For i = 1 to len(word)

if mid(word, i, 1) = "t" Then tCount = tCount + 1

Next i

if tCount > 1 Then

if len(join(tWordArray)) = 0 Then

Redim preserve tWordArray(0)

else

Redim preserve tWordArray(Ubound(tWordArray) + 1)

End if

tWordArray(Ubound(tWordArray)) = word

End if

Next word

The code above splits the checkString on each space. The Split method will
automatically split the string on spaces if not speciBed. It can split on any
indicator whether is be commas, dashes, you get the point. The Split
method returns an array of strings.

We loop through the array of words. Then we loop through each character
in the word by using the mid method which takes the a segment of a string.
We specify that we want to take one character from the word string at
character i. Note that the mid method takes the character number and not
the index, therefore we run from 1 to the length of the word. If the
character at i is t we increment the tCount variable.

If the tCount is greater than 1 we add the word to the tWordArray. We Brst
check if there is anything in the array by checking the length of all items in
the array. If the length is 0 then we know there are no items. We then can
set the dimension to be 0. If the length is not 0 then there is already an item
in the array and we call the Ubound method to Bnd the number of indices
in the array.

We end with having an array of strings. The strings should be “matter” and
“scatter”. This example explores how to identify parts of a string. We can
also manipulate the string. For example we could have changed each t to an
a. There are many possibilities with string manipulation and this is a quick
example to show the power of strings.

Programming Vba Excel Corporate Innovation Automation

More from breakCorporate Follow

Writing about the shortcomings of Corporations, the breakdown in the US


education system, and why you shouldnt be afraid to take risks.

Jul 10, 2016

VBA — Functions vs. Subs


There are two methods in VBA — functions and subs. A sub does not return
any value while a function must return a value. Functions and subs can be
either Private or Public. When either is private they can only be called
within the current module. If it is in a class then only functions and subs
within the class can call it. The function or sub would not appear in the
Macros list available to the end user. …

Read more · 2 min read

Jul 10, 2016

VBA — Bubble Sort


A bubble sort is a technique to order items in an array. They can be set in
either ascending or descending order. It is possible to output items in an
array to Excel and then call the Sort method on that range. However, this is
not best practice.

We have the following array:

Dim sortArray(9) as Integer


sortArray(0) = 100
sortArray(1) = 20
sortArray(2) = 30
sortArray(3) = 70
sortArray(4) = 10
sortArray(5) = 40
sortArray(6) = 90
sortArray(7) = 80
sortArray(8) = 60
sortArray(9) = 50

For i = 0 to Ubound(sortArray)

For x = Ubound(sortArray) to i…

Read more · 2 min read

Jul 10, 2016

VBA — Add Items to an Array


Arrays in VBA are a little Bnicky and require a diTerent procedure than
programming langauges like C or Python. In VBA you cannot append an
item to an array. Instead you must re-dimension the array to be one index
larger than the previous.

To initialize the array:

Dim arr() as String

To add an item to the array:

Redim preserve arr(0)


arr(0) = string1

Redim preserve arr(1)


arr(1) = string2

or

Redim preserve arr(Ubound(arr) + 1)


arr(Ubound(arr)) = string2

You cannot check the Ubound (upper bounds) of an array if it does not have
a dimension.

It is also important…

Read more · 1 min read

84 1

Jul 10, 2016

Automate Excel Monotony


Ah, end of the month again and I need to setup those fucking reports. Every
month it’s the same thing with one little change. Fuck it, it’s the same thing.
Input the data into this sheet, run that model, update a sheet. There it is.
One of 20 reports is done. Oh sorry, I don’t want you to think updating a
report was as quick and simple as that short sentence. No, this is a
painstaking task that requires double checking every piece before and after.
Now if that process fails in the model, well shit, that adds another
headache…

Read more · 2 min read

About Help Legal

You might also like