Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which command in VBA can count the number of characters in a string variable?

Let's say I have this variable:

word = "habit"

which command in VBA will allow me to count how many characters are there in this variable (in my case it's 5).

Important: the variable "word" contains only one word, no spaces, but may have contain numbers and hyphens.

like image 485
brilliant Avatar asked Nov 12 '09 20:11

brilliant


People also ask

How do I count the number of characters in a string in VBA?

Formula property of VBA Excel to count the total number of characters in a range of cells. The Len function counts the number of characters in each of the cells of the selected range and the SUMPRODUCT function sums the numbers to output the total.

How do I count the length of a string in VBA?

THE VBA LEN function returns the “length of the string” and the number of characters in the supplied value. Of all the string functions in VBA. There are numerous string functions in VBA, all of which are classified as string or text functions. read more, “LEN” is the most under-utilized function.

How do you use count in VBA?

Step 1: Go to Insert menu tab and click on Module option as shown below from the list. Step 2: Select the range of cell where we want to see the output. Here that cell is C12. Step 3: Now use the count function in inverted commas in select the range of those cells which we need to count.


3 Answers

Try this:

word = "habit"
findchar = 'b"
replacechar = ""
charactercount = len(word) - len(replace(word,findchar,replacechar))
like image 111
user5053510 Avatar answered Oct 17 '22 05:10

user5053510


Do you mean counting the number of characters in a string? That's very simple

Dim strWord As String
Dim lngNumberOfCharacters as Long

strWord = "habit"
lngNumberOfCharacters = Len(strWord)
Debug.Print lngNumberOfCharacters
like image 26
Ben McCormack Avatar answered Oct 17 '22 05:10

Ben McCormack


Len(word)

Although that's not what your question title asks =)

like image 10
David Hedlund Avatar answered Oct 17 '22 06:10

David Hedlund