Convert To Text- Easy Excel VBA Macro.


Hello Excellers and welcome back to another #MacroMonday #Excel tip where I share simple Excel Macros which will save you a lot of time working in Excel. Do you need to convert numbers to text on a regular basis?. Is it painful?. Well, let’s write some simple code to convert your selected range of cells to text with one simple click.

Preparing To Write The Code.

First, you will need to open the Visual Basic Editor.  There are two ways to do this.  Either by hitting ALT +F11 or selecting the Developer Tab | Code Group | Visual Basic.  Both methods have the same result.   You then have a choice, you can either create a module to store your code either in your Personal Macro Workbook or in your current workbook.  What’s the difference?.  If you save you code in your Personal Macro workbook it will be available for use in any of my Excel workbooks.  If you store it in the current workbook then use is restricted to that workbook.

As you can see this code will be useful to reuse in any workbook. I will create and save this for future use in my Personal Macro Workbook.

Learn More About Your Personal Macro Workbook (PMW)

If you want to read more about your Excel PMW then check out my blog posts below.

Macro Mondays -Creating and Updating Your Personal Macro Workbook

Macro Mondays – Create A Shortcut To Your Personal Excel Macro Workbook

Why Is My Personal Macro Workbook Not Loading Automatically?

Starting The Macro.

We need to start off the macro by inserting a New Module.  Do this by selecting the Personal.xlsb workbook, then Insert Module.  Type Sub then the name of your macro.  In this example, I have called it simply ConvertToText.  Notice that Excel will automatically enter the end text End Sub to end the Sub Routine.  We simply need to enter the rest of the code between these two lines.

[stextbox id=’info’]
Sub ConvertToText
End Sub
[/stextbox]

Declaring Variables.

We now need to declare only one variable for this Function. This simply creates a memory container for the range of cells we want to convert to text. We also set the range as the cells that have been selected by the user.

[stextbox id=’info’]
Dim rng As Range
Set rng = Selection
[/stextbox]

Converting The Range To Text.

Excel now takes the selected range of cells and converts it to text with this simple line of code. (I told you it was easy didn’t I?)

[stextbox id=’info’]
rng.NumberFormat = “@”
[/stextbox]

Ending The Macro.

Finally, the macro ends once the selected range of cells has been formatted as text. Excel had already generated this part of the code when we named the function at the beginning of the code.

[stextbox id=’info’]
End Sub
[/stextbox]

Testing The Macro.

Now all that is left is to test the macro.


That is one quick and easy way to use VBA to Change Format of Cell to Text.

What Next? Want More Tips?

So, if you want more tips then sign up for my Monthly Newsletter where I share 3 Tips on the first Wednesday of the month and receive my free Ebook, 30 Excel Tips.

1If you want to see all of the blog posts in the Macro Monday series. Click on the link below

Macro Mondays Blog Posts.

Learn Excel With Our Excel Online Courses

Do You Need Help With An Excel Problem?.

Finally, I am pleased to announce I have teamed up with Excel Rescue, where you can get help FAST.  All you need to do is choose the Excel task that most closely describes what you need to be done.  Above all, there is a money back guarantee and similarly Security and Non-Disclosure Agreements.  Try It!.  Need Help With An Excel VBA Macro?.  Of course, you don’t need to ask how to list all files in a directory as it is right here for free.

ExcelRescue.net

Udemy.com Home page 125x125

Barbara

Barbara is an experienced data analyst with over 20 years of experience. Author of the How To Excel At Excel Top 50 Excel tips Ebook, YouTuber and Excel Blogger.

Recent Posts