Convert Numbers To Text Macro

Draperies

Board Regular
Joined
Jun 29, 2009
Messages
79
I have an excel sheet with a list of numbers that I would like to convert from the number format to text format where all the numbers are 5 characters and preceded by "0"s. In excel I am able to use the =TEXT(A2,"00000") function to achieve the desired results... so for example the number 2 would become "00002" and 847 would become "00847". However, I cannot achieve the same results in my macro with the following code:

Code:
LRow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
i = 2
Do While i <= LRow
    Cells(i, 1).Value = WorksheetFunction.Text(Cells(i, 1).Text, "00000")
    i = i + 1
Loop
This simply produces the same values and even format in the cells it works on. Does anyone have any suggestions on how to handle this?
 
Need some help need to take number data as 12.34
1) remove the decimal and convert to text
2) make sure if it 55 that when I remove the . is comes out as 5500
3) Right align
4) Zero fill to seven digits.

Thanks Heather






Code:


Sub ConvertID() ' ' ConvertID Macro ' Convert ID# to text ' ' Keyboard Shortcut: Ctrl+q Dim i As Long, _ LR As Long LR = Range("A" & Rows.Count).End(xlUp).row Range("A1:A" & LR).NumberFormat = "@" Application.ScreenUpdating = False For i = 1 To LR Application.StatusBar = "Currently on row " & i & " of " & LR Cells(i, 1).Value = Format(Cells(i, 1).Value, "0000000") Next i Application.ScreenUpdating = True Application.StatusBar = False End S</pre>
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
MrKowz,

Can you show me how to change this to work on whatever cells I currently have selected in excel instead of just column a?
 
Upvote 0
MrKowz,

Can you show me how to change this to work on whatever cells I currently have selected in excel instead of just column a?

Try:

Code:
Public Sub FixNumbers()
Dim rng As Range

Application.ScreenUpdating = False

rng.NumberFormat = "@"
For Each rng In Selection
    rng.Value = Format(rng.Value, "00000")
Next rng
 
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,956
Messages
6,122,465
Members
449,085
Latest member
ExcelError

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top