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

Inserting a Document's Path

You can use the FILENAME field to insert a document's filename and, optionally, the path to that filename. However, if you ...

Discover More

Controlling Overtype Mode

Some people like to have Word replace previous information as they type; this is called "overtype mode." You can control ...

Discover More

Adding Page Numbers in Headers or Footers

While Word has a default format for page numbers, you can design and specify how you want them to appear in your document. ...

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)

Converting Phone Numbers

Sometimes you receive a phone number that contains alphabetic characters and you need to convert it to a purely numeric phone ...

Discover More

Removing All Macros

Macros are stored as part of a workbook so that they are always available when you have the workbook open. If you want to get ...

Discover More

Copying Worksheet Code Automatically

When creating a workbook to be used by others, you may want any worksheets they add to the workbook to contain some special ...

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. Maximum image size is 8Mpixels. 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 7 - 0?

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.