Excel

Add Leading Zeros

Ease of Use

Easy

Version tested with

Excel 2003 

Submitted by:

Oorang

Description:

This sub will add your choice of leading zeros to you selection. Also included is the ability to pad by delimiter (ex: 1-1-1 = 01-01-01). 

Discussion:

Some systems make use of leading zeros in account numbers. For instance all NCPDP/NABP numbers are 7 digits. So 53454 would be represented as 0053454. Unfortunately it is easy to "loose" the leading zeros in Excel, making in necessary to add them back in. (Particularly an issue if you intend to import the file into a database later.) The PadCells example shows you how to programmatically restore leading zeros to a range of data. (The select range to be specific.) Also included is an example that will pad to segments. This will pad each segment you specify to the desired length. An example would be an application that is storing date data as text. It may have all dates as 07-03-17. But you have received the data as 7-3-17. If you use the hyphen ("-") as the delimiter you have 3 segments 7 3 & 17. If you specify the segment length as 2 then it will return 07-03-17. Special Note: You can also add in Leading Zeros using this Excel Formula =REPT("0",10-LEN(A1))&A1 where 10 is the desired length. 

Code:

instructions for use

			

Option Explicit Sub PadCells() On Error Goto Error_Handler Dim Cll As Excel.Range Dim Data As Excel.Range Dim TargetLength As Long Dim CellLen As Long Dim CellValue As String Dim Truncate As VBA.VbMsgBoxResult Const ForceText As String = "'" Const Zero As String = "0" Const LengthError As Long = 5 Set Data = Excel.Selection TargetLength = Excel.Application.InputBox( _ "Enter a numeric value indicated the desired length of data in characters:", _ "Enter Target Cell Length", VBA.Len(Data.Cells(1, 1).Value), Type:=1) If TargetLength = 0 Then 'Detect Cancel MsgBox "Operation Aborted", vbInformation + vbMsgBoxSetForeground Exit Sub End If For Each Cll In Data.Cells If Not Cll.HasFormula Then CellValue = Cll.Value CellLen = VBA.Len(CellValue) Cll.Value = ForceText & _ VBA.String$(TargetLength - CellLen, Zero) & CellValue End If Next Cll Exit Sub Error_Handler: If VBA.Err.Number = LengthError Then Truncate = VBA.MsgBox("Encountered cell at " & Cll.Address & _ " with length of " & CellLen & _ ", do you wish to truncate it's value to " & TargetLength & _ " characters? (If you select the new value will be """ & _ VBA.Right$(CellValue, TargetLength) & """.)", _ vbQuestion + vbYesNoCancel + vbMsgBoxSetForeground, "Invalid Length") If Truncate = vbCancel Then MsgBox "Operation Aborted", vbInformation + vbMsgBoxSetForeground Exit Sub ElseIf Truncate = vbYes Then Cll.Value = ForceText & VBA.Right$(CellValue, TargetLength) End If Resume Next End If VBA.MsgBox VBA.Err.Number, vbCritical + vbMsgBoxSetForeground + _ vbMsgBoxHelpButton, "Error: " & VBA.Err.Number, VBA.Err.HelpFile, _ VBA.Err.HelpContext End Sub Sub PadSegments() On Error Goto Error_Handler Dim Cll As Excel.Range Dim Data As Excel.Range Dim TargetLength As Long Dim SegmentLen As Long Dim Truncate As VBA.VbMsgBoxResult Dim Delimiter As String Dim TmpArray As Variant Dim SegmentIndex As Long Const ForceText As String = "'" Const Zero As String = "0" Const LengthError As Long = 5 Set Data = Excel.Selection TargetLength = Excel.Application.InputBox( _ "Enter a numeric value indicated the desired length of data in characters:", _ "Enter Target Cell Length", VBA.Len(Data.Cells(1, 1).Value), Type:=1) If TargetLength = 0 Then 'Detect Cancel MsgBox "Operation Aborted", vbInformation + vbMsgBoxSetForeground Exit Sub End If Delimiter = VBA.InputBox("Enter the delimter to pad to:", "Enter Delimiter", "-") If Delimiter = vbNullString Then 'Detect Cancel MsgBox "Operation Aborted", vbInformation + vbMsgBoxSetForeground Exit Sub End If For Each Cll In Data.Cells If Not Cll.HasFormula Then TmpArray = VBA.Split(Cll.Value, Delimiter) For SegmentIndex = Zero To UBound(TmpArray) SegmentLen = VBA.Len(TmpArray(SegmentIndex)) TmpArray(SegmentIndex) = VBA.String$(TargetLength - SegmentLen, Zero) & TmpArray(SegmentIndex) Next Cll.Value = ForceText & VBA.Join(TmpArray, Delimiter) End If Next Cll Exit Sub Error_Handler: If VBA.Err.Number = LengthError Then Truncate = VBA.MsgBox("Encountered cell at " & Cll.Address & _ " with length of " & SegmentLen & _ ", do you wish to truncate it's value to " & TargetLength & _ " characters? (If you select the new value will be """ & _ VBA.Right$(TmpArray(SegmentIndex), TargetLength) & """.)", _ vbQuestion + vbYesNoCancel + vbMsgBoxSetForeground, "Invalid Length") If Truncate = vbCancel Then MsgBox "Operation Aborted", vbInformation + vbMsgBoxSetForeground Exit Sub ElseIf Truncate = vbYes Then TmpArray(SegmentIndex) = VBA.Right$(TmpArray(SegmentIndex), TargetLength) End If Resume Next End If VBA.MsgBox VBA.Err.Number, vbCritical + vbMsgBoxSetForeground + _ vbMsgBoxHelpButton, "Error: " & VBA.Err.Number, VBA.Err.HelpFile, _ VBA.Err.HelpContext End Sub

How to use:

  1. Press F11 to launch the VBE.
  2. Insert a module.
  3. Paste Code
 

Test the code:

  1. Select a range
  2. Press Alt F8 to run macros
  3. Select this macro
 

Sample File:

Example Worksheet.zip 10.59KB 

Approved by mdmackillop


This entry has been viewed 308 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express