<

Add letter before content in a cell (excel macro)

Published on
12,465 Points
6,465 Views
Last Modified:
Approved
I've recently been in need of an Excel macro that could add a letter before the text on multiple cells in an Excel document.

My English is as it is, so I will try explain what it does diffrently.

If you have an excel document with 2000 rows and you need to add an "A" before every row under column "A", this is what you can do.

First you'll need to create a new macro in excel, when you do the "Microsoft Visual Basic Editor" will open.

In the editor, paste this code (also included below):
--------------------------------------
Sub AddLetterMacro()

For x = 1 To 2000

    ActiveCell.FormulaR1C1 = "THE LETTER" + ActiveCell.Text
   
    ActiveCell.Offset(1, 0).Select

Next x
End Sub
--------------------------------------

At this row:

For x = 1 To 2000

change the value "2000" to the number of cells in your document.

then go to:

ActiveCell.FormulaR1C1 = "THE LETTER" + ActiveCell.Text

and change "THE LETTER" to any letter you want to be placed before every cell in that row.

Now go to your Excel document and click in the cell where your should start, then go to the editor again and press the "Play(>)" button or just press "F5" and the macro will go off.

This macro could probably be done much better to count the rows in the document etc. but it's an easy way to go.

You could also add an letter to the end of the cell instead of the beginning by simply changing:

ActiveCell.FormulaR1C1 = "THE LETTER" + ActiveCell.Text

to

ActiveCell.FormulaR1C1 = ActiveCell.Text + "THE LETTER"

Hope this article will be to some use for you.

And also, this macro has been tested on both MS Excel 2003 and MS Excel 2007.

// Tobias
0
Author:Gudorian
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Get 7 days free