Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
601 views

How To Loop Through Each Character Within A String

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)
601 views

How To Loop Through Each Character Within A String

Uploaded by

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

9/27/2020 How to loop through each character within a string

U a

How to loop through each character


within a string
by Ilker | Apr 18, 2018 | Excel Macros, Excel Tips & Tricks

You’ve worked with text data before, used formulas, and created reports, but how do you
up your game? E ciently loop through text data using this simple VBA code to analyze or
manipulate strings.

Code

1 Sub LoopThroughString()
2 Dim counter As Integer
3 Dim mystring As String
4 mystring = "Excel is good" 'define string
5 For Counter = 1 To Len(mystring) 'count from 1 to length of string
6 Debug.Print Mid(mystring, Counter, 1) 'do something to each charact
7 Next
8 End Sub

How

https://www.spreadsheetweb.com/loop-characters-string/ 1/4
9/27/2020 How to loop through each character within a string

First, we need to de ne the variables. Integer counter, string mystring, and set mystring
a text value. Here we used a static text value here, but you can set dynamic values to cells
by using the RANGE() function. For example,

mystring = Range(“A1”).Value.

Next, we need to use a For-Next Loop to iterate each character in mystring. Counter
variable gets an incremental value for each character, from 1 to the length of the string
variable (i.e. Len(mystring)). On every step of the For-Next Loop, a character in
mystring will be caught by the Mid function. Mid works in same way as the MID formula,
it returns a string part from a source parameter. Counter indicates the number of
characters and 1 is the length of the string to grab.

Here, we used Debug.Print method for demonstration purposes. This argument prints the
strings into an Immediate window.

Related Posts:
1. How to unhide sheets in Excel with VBA
2. How to display cell formulas in Excel that contain errors using VBA
3. How to make Excel change case to uppercase using VBA
4. How to make Excel delete all pictures

How to count the number of cells less than the average in Excel

Function: CELL

How to create a pizza chart in Excel

How to create a worksheet selection popup in Excel

How to calculate the percent of completed month

Function: YEARFRAC

How to calculate the percent of completed year

How to create a thermometer chart in Excel

How to modify a Comment Box in Excel

https://www.spreadsheetweb.com/loop-characters-string/ 2/4
9/27/2020 How to loop through each character within a string

How to show workbook location in Quick Access Toolbar

Product
Features

Pricing

Demos

Customers

Support
User Guide

FAQ

Training Videos

Case Studies

Tips

Resources
Blog

Excel Applications

Excel Tips & Tricks

Excel Formulas

Excel Macros

Excel Templates

https://www.spreadsheetweb.com/loop-characters-string/ 3/4
9/27/2020 How to loop through each character within a string

About
About Us

Contact Us

Services

Press Releases

 
© Pagos, Inc. - All rights reserved - Privacy Policy - Terms of Use

https://www.spreadsheetweb.com/loop-characters-string/ 4/4

You might also like