VBA To Convert Number to Text, Keeping Leading Zeros

AndyPandy27

Board Regular
Joined
Jul 17, 2012
Messages
142
Hi All,

I'm very new to VBA, but have been experimenting to get a bit better, however I'm stuck and would greatly appreciate some help.

I have ~85,000 rows of data in a "Master Price List", with Product Codes listed in B5:B85000.

However, some of the Product Codes, all of varying length, are currently in a custom number format, to ensure leading zeros are displayed, where applicable. The customer number format is:

Code:
[>9999]000000;General

In order to convert these to text as "quickly" as possible (please let me know if I am doing this all wrong!), I have written the following VBA:

Code:
Sub SingleQuote()           
 For Each Cell In Selection
  If Not Cell.HasFormula Then
   Cell.Value = Chr(39) & Cell.Value
  End If
 Next Cell
End Sub

This works to convert the numbers to text, but it removes the leading zero for those codes which currently have it added based on my custom number format.

Is there any way of being able to amend the above code, to ensure that the numbers which need to have a leading zero, continue to do so?

The criteria for which cells need a leading zero are as follows:

1. The cell in Column G of the same row as the Product Code in Column B = 50
2. The length of the Product Code in Column B = 6

Please let me know if you need any further information from me.

Many thanks,

AP
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi

If I understand correctly you are happy with the text displayed in the cells.
If that's the case you can use it as the value for the cells.

Try:

Code:
Sub SingleQuote()
Dim r As Range, s As String
 
For Each r In Selection
    If Not r.HasFormula Then
        s = r.Text                     ' gets text displayed in cell
        r.NumberFormat = "@"     ' sets cell format as text
        r.Value = s                    ' writes the new value in the cell
    End If
Next r
End Sub
 
Upvote 0
see if this works
Code:
Sub SingleQuote()           
   For Each Cell In Selection
       If Not Cell.HasFormula Then
            Cell.Value = cstr(format(Cell.Value,"000000"))
       End If
   Next Cell
End Sub
 
Upvote 0
see if this works
Code:
Sub SingleQuote()          
   For Each Cell In Selection
       If Not Cell.HasFormula Then
            Cell.Value = cstr(format(Cell.Value,"000000"))
       End If
   Next Cell
End Sub
@diddi This is awesome! Was able to use it when I was joining to cells (a date) and some text ("->") and row no. and get the row number in the format ("0000"). Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

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