Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Creating a String in a Macro.
Written by Allen Wyatt (last updated November 20, 2021)
This tip applies to Excel 97, 2000, 2002, and 2003
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:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2325) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Creating a String in a Macro.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
If you need to find out how many columns are set to be a specific width, you'll need a macro to help determine the info. ...
Discover MoreIf you are using a macro to process a number of worksheets, you may have a need to know if the worksheet is empty or not. ...
Discover MoreYou can easily configure Excel so that it runs a specific macro whenever a worksheet is deactivated. Just follow the easy ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments