LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default Add leading zero(s) to cell values in selection - An Example

No question here, just an Excel VBA procedure for the archive.

Search Criteria: Add leading zeros to number, cell value, lead cell
value with zeros, precede value with zero, add zero before value, and
concatenate number with zero

Sub LeadingZeroAddPrompt()

'Prompts user and adds the leading zeros to cell value for all
'cells in selection. Note this procedure must format values as TEXT
'for the procedure to work properly.


Dim v As Integer
On Error Resume Next
v = InputBox("Enter # of zeros to add to front. Five (5) is the
most you can add.", "Add Leading Zeros")

If v = 1 Then
For Each cell In Selection
cell.NumberFormat = "@"
cell.Value = "0" & cell.Value
Next cell
End If

If v = 2 Then
For Each cell In Selection
cell.NumberFormat = "@"
cell.Value = "00" & cell.Value
Next cell
End If
If v = 3 Then
For Each cell In Selection
cell.NumberFormat = "@"
cell.Value = "000" & cell.Value
Next cell
End If
If v = 4 Then
For Each cell In Selection
cell.NumberFormat = "@"
cell.Value = "0000" & cell.Value
Next cell
End If
If v = 5 Then
For Each cell In Selection
cell.NumberFormat = "@"
cell.Value = "00000" & cell.Value
Next cell
End If
End Sub
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Limiting selection in a cell AND linking that selection to a list Lisa Excel Discussion (Misc queries) 1 July 28th 09 05:00 PM
Range of Values with different column selection CharlieR Excel Discussion (Misc queries) 1 June 3rd 09 07:14 AM
Formatting cell based upon values w/in a selection Carissa New Users to Excel 1 December 17th 08 07:04 PM
sequential numbering of zero leading values nagorb Excel Discussion (Misc queries) 1 June 8th 05 04:58 AM
Leading Zeros in Numeric Values DBavirsha Excel Worksheet Functions 6 January 4th 05 05:21 PM


All times are GMT +1. The time now is 05:01 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"