Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Creating a String in a Macro.

Creating a String in a Macro

Written by Allen Wyatt (last updated January 6, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


What do you do if you need a string of 80 equal signs or 25 spaces in your macro? Use the String function. This function is used to create strings of repeating characters. Consider the following examples:

sNew1 = String(25, 32)
sNew2 = String(25, " ")
sNew3 = String(80, "=")
sNew4 = String(20, "=*")

The first and second lines are functionally the same; they both produce a line of 25 spaces. In the first example, the ANSI value of 32 is used, which is the character code for a space. In the third line, sNew3 will be equal to 80 equal signs.

The fourth line produces a 20-character string of equal signs. This can be a bit frustrating to programmers familiar with other implementations of BASIC, as to them the last example should create a 40-character string of alternating equal signs and asterisks. (Under older versions of BASIC, the String function concatenates whatever you designate, so one could expect this to create a 40-character string made up of 20 iterations of "=*". Not so; VBA does not implement the String function as is done in other BASICs.)

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (7801) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Creating a String in a Macro.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Jumping to the Real Last Cell

Jumping to the last cell in a worksheet should be easy, but you may not always get the results that you expect. This tip ...

Discover More

Microsoft Word's Amazing Autos

Word provides several tools that can aid in developing your documents. This e-book focuses on a few of those tools. You ...

Discover More

Creating Categories for Your Table of Authorities

A table of authorities is normally divided into separate sections based on categories you define. Here's how to create ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Selecting a Cell in the Current Row

Macros often need to select different cells in a worksheet. Here's how you can use macro commands to change which cell is ...

Discover More

Displaying the Print Dialog Box in a Macro

Want to print a document by using a macro? One way is to display the Print dialog box and allow the user to interact with ...

Discover More

Controlling Window Size when Opening Additional Workbooks

When you open multiple workbooks, the way in which Excel sizes them is not the best for your needs. This tip looks at a ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 5 - 3?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.