Excel VBA – Loop Characters in Cell

The following macro will define a range of cells, then loop through each cell, interrogating each character.  This works well if you are trying to determine how many instances of a given condition exist in each cell.

The actual VBA code is quite simple and can be copied into your VBA Project (modify to suit your needs).  As always, I encourage you to copy down the code and give it a try for yourself.

For simplicity, say you have the following table in Excel:

Excel VBA - Loop Characters in Cell

Run the macro “LoopCellCharacters”, and you will see the following results.  A count of each instance appears in Column B, next to each cell interrogated (In this case, how many cells have the Red font):

Excel VBA - Loop Characters in Cell

Update the macro to check for a specific letter:

Run the macro “LoopCellCharacters” again, and you will see the following results.  A count of each instance appears in Column B, next to each cell interrogated (In this case, how many cells have the letter “i” ):

Excel VBA - Loop Characters in Cell

This comes in handy when you are trying to find specific conditions within the cells of a given range.  You can modify this macro to meet your needs.

 

Additional Content

Check out more examples by visiting my Home Page

Here you will find topics covering  Qlikview  SQL Server  Excel VBA

 

Leave a Comment