Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. 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

by Allen Wyatt
(last updated May 17, 2017)

6

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.)

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (7801) applies to Microsoft Excel 2007, 2010, and 2013. 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

Turning Off Automatic Capitalization

When you start typing, do you ever notice those times that Word starts capitalizing words at the start of what it thinks are ...

Discover More

Adjusting Date Values by Keypress

Excel allows you to store dates in a cell. Wouldn't it be great if you could select a cell containing a date and then ...

Discover More

Setting a Length Limit on Cells

Limiting what can be entered in a cell can be an important part of developing a worksheet that other people use. Here are a ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Clearing the Undo Stack in a Macro

Excel keeps track of the actions you take so that you can undo those actions if any are taken in error. You may want to clear ...

Discover More

Selecting a Range of Cells Relative to the Current Cell

When processing information in a macro, you often need to select different cells relative to the currently selected cells. ...

Discover More

Getting a File Name

Does your macro need to allow the user to specify a particular file name that should be used by the macro? Here's a quick ...

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}] in your comment text. You’ll be prompted to upload your image when you submit the comment. 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 five less than 5?

2015-10-27 00:11:53

David Richardson

@PhilP putting the single quote before the equal sign only produces a string of 80 single quotes.
Aldo's solution applied to sNew3 works as follows:
sNew3 = "'" & String(80,"=")


2013-08-12 11:59:38

Aldo

Have you tried using =REPT() function.

=REPT("=",80) in a cell will give you ================================================================================ in the cell without an error raised.

sNew4 = "'" & String(80, 61) give you the same result in VBA. The "'" is needed because Excel sees = as a start of a function.

You can change the cell format to TEXT and illiminate the need for "'" altogether.


2013-07-26 14:07:19

awyatt

The last two only return errors if you try to place the contents of the variables (sNew3 and sNew4) into a cell in a worksheet. That is because Excel's formula parsing is only done when values are entered in a worksheet.

If you are using the VBA String function to create a string (as done in this tip) and want to stuff that string into a cell, then you'll need to do as PhilP suggests: put an apostrophe at the beginning of the string.

-Allen


2013-07-26 13:33:31

PhilP

@David

As you say the last two return errors. The way around this to include a single quote at the beginning of the string after the first double quote, for example:

sNew3 = String(80, "'=")

This makes sure that Excel knows all of the string is text


2013-07-26 05:55:47

Rudra Sharma

Thanks for letting me know this.


2013-07-25 19:37:07

David Richardson

I tried out the examples, but the last two return errors. I think that since Excel uses the = to signify a formula that is the cause. (even the ASCII code failed)


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.