How to use the code?

R

Raja

Hi Patrick

Thanks for giving me the formula to add 99 before the digit its working
perfectly but i could not make use of the code which you hav esuggested,
i have inserted the code by right clicking the sheet1- viewcode and
pasted the code but i could not able to run the macro.

So please help as im new to excel.

The code is:

dim cell as range
dim text as string
set cell = range("A1")
do until isempty(cell)
text = cell.value
cell.value = LEFT("990000000000",12 - len(text) ) & text
set cell = cell.offset(1)
loop
 
R

Rick Rothstein

In order to run any code in VB, it has to be "housed" in a Sub or Function
header at the beginning and an End Sub or End Function at the end. Your code
looks like it was intended to be run as a macro, so it should have been
posted this way...

Sub Add99ToSerialNumber()
Dim Cell As Range
Dim Text As String
Set Cell = Range("A1")
Do Until IsEmpty(Cell)
Text = Cell.Value
Cell.Value = Left("990000000000", 12 - Len(Text)) & Text
Set Cell = Cell.Offset(1)
Loop
End Sub

To run this code, press Alt-F8 from the worksheet where you want to work on
and pick Add99ToSerialNumber from the list that appeared and then click the
Run button.
 
J

JLGWhiz

Hi Raja, I believe that Patrick assumed you would know to add the beginning
and ending lines to the macro as shown below. To run the macro, click
Tools>Macro>Macros, select the macro name and click "Run".

Sub prfx99() '<===Macro name

Dim cell As Range
Dim text as String
Set cell = Range("A1")
Do Until IsEmpty(cell)
text = cell.value
cell.value = LEFT("990000000000",12 - len(text) ) & text
Set cell = cell.offset(1)
Loop

End Sub '<===Closes macro
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top