Converting Microsoft Excel Values to Text in an Excel Macro

Last reviewed: February 2, 1998
Article ID: Q64681

The information in this article applies to:
  • Microsoft Excel 98 Macintosh Edition
  • Microsoft Excel for Windows 95, version 7.0
  • Microsoft Excel for Windows, versions 2.x, 3.0, 4.0, 5.0
  • Microsoft Excel for the Macintosh, versions 2.x, 3.0, 4.0, 5.0

SUMMARY

The following macro command will convert a numeric value in the active cell to a text string:

   =FORMULA("="&CHAR(34)&ACTIVE.CELL()&CHAR(34))

For example, when you run this command on a cell that contains the following entry:

   3

the entry is converted to the following:

   ="3"

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/default.asp

The follow macro example converts either a single cell or a range of cells from numeric data to text so that you can affect multiple cells at one time.

   A1: Convert_number_to_text
   A2: =FOR.CELL("CURRENT",,TRUE)
   A3: =FORMULA("="&CHAR(34)&CURRENT&CHAR(34),CURRENT)
   A4: =NEXT()
   A5: =RETURN()

Microsoft Excel Version 4.0

You can assign this macro to a button in Microsoft Excel version 4.0 by using the following steps:

   1. From the Options menu, choose Toolbars.

   2. Choose the Customize button.

   3. From the Categories list, select Custom.

   4. From the list of buttons, drag the button that you want to use to run
      the macro to the toolbar area.

   5. From the Assign Macro list in the Assign To Tool dialog box, select
      Macro1!Convert_number_to_text (where Macro1 is the macro sheet that
      contains the macro, and choose OK.

   6. Close the Customize dialog box.

   7. Save the macro sheet, Macro1 in this example, to the XLSTART
      subdirectory (Windows) or the EXCEL STARTUP FOLDER (4) (Macintosh).

   8. After you save the file, choose Hide from the Window menu to hide the
      macro sheet. When you exit Microsoft Excel, choose Yes to save
      the changes to the macro sheet.

The macro sheet will automatically open when you start Microsoft Excel, so that you can run the macro from the custom toolbar button that you created.

Microsoft Excel Versions 5.0 and later

NOTE: This macro command also applies to Microsoft Excel versions 5.0 and 7.0, but is not required, because of the ability to format cells as text. To format cells as text in Microsoft Excel version 5.0 and 7.0, do the following:

   1. Select the cell(s) that you want to format as text.

   2. On the Format menu, click Cells. Select the Number tab.

   3. In the Category list, click Text, and then click OK.

Numbers that you enter in these cell(s) will be formatted as text.

NOTE: If you format a cell as Text in Microsoft Excel versions 5.0 and 7.0, the underlying cell value will still be treated as a number, if that is what you entered in the cell. The following Visual Basic for Applications macro will convert underlying cell values to Text.

   Sub Convert2Text()

      For Each Cell in Selection.Cells

         OldNF$ = Cell.NumberFormat

         Cell.NumberFormat = "@"

         Cell.Value = Application.Text(Cell.Value, OldNF$)

      Next

   End Sub

To use this macro, select the range of cells you want converted to text and then run the macro.


Additional query words: 2.00 2.01 2.10 2.20 2.21 3.00 4.00 5.00
Keywords : kbcode kbprg
Version : WINDOWS:2.x,3.x,4.0,5.0,7.0; MACINTOSH:2.x,3.0,98
Platform : MACINTOSH WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 2, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.