Dim aStartTime
Dim ProcName As String
Dim bErrorHandle As Boolean
Const DblSpace As String = vbNewLine & vbNewLine
Const CoName As String = "Pading Text"
Sub PadText()
Dim v As Long, vLWRs As Variant, vLWRs1
Dim rng As Range
Dim iCount As Long
Dim NumOfDigits As Long
Dim rngLen As Long
Dim rngValue As String
Dim strText As String
Const cNumOfDigits As Long = 3
Const cText As String = "0"
1 On Error GoTo errhandler
2 bErrorHandle = False
'~~> Code Starts from here
3 With ActiveSheet
'~~> Define the selected range
4 Set rng = Selection
'~~> Capture users numbers of digits which is padded with leading zeros
5 NumOfDigits = Application.InputBox( _
"Enter a numeric value indicated the desired length of data in characters:", _
"Enter Target Cell Length", cNumOfDigits, Type:=1)
'~~> Detect Cancel
6 If NumOfDigits = 0 Then
7 MsgBox "Operation Aborted", vbInformation + vbMsgBoxSetForeground
8 Exit Sub
9 End If
'~~> Capture users Text which is padded with leading Text
10 strText = Application.InputBox("Enter the text to pad to:", "Enter Text", cText)
'~~> Detect Cancel
11 If strText = vbNullString Then
12 MsgBox "Operation Aborted", vbInformation + vbMsgBoxSetForeground
13 Exit Sub
14 End If
'~~> Start Timer
15 aStartTime = Now()
'~~> Speeding Up VBA Code
16 Call SpeedUp(False)
17 With rng
18 If .Cells.Count > 1 Then
'~~> Change number format to Text
19 .NumberFormat = "@"
'~~> Pass values from Spreadsheet to an array to conduct processing faster
'~~> Creates Variant(1 to 2, 1 to 1)
20 vLWRs = .Value
'~~> Loop thru array in computer memory and round numbers
21 For v = LBound(vLWRs, 1) To UBound(vLWRs, 1)
22 rngValue = vLWRs(v, 1) '.Value
23 rngLen = VBA.Len(rngValue)
'~~> Update items in array
24 On Error GoTo qSkip1
25 vLWRs(v, 1) = VBA.String$(NumOfDigits - rngLen, strText) & rngValue
'Application.Text(vLWRs(v, 1), Application.Rept(strText, NumOfDigits))
qSkip1:
26 Next v
'~~> Write Array To Spreadsheet
27 .Cells = vLWRs
28 Else
'~~> Redim array size to 1 (only 1 cell in range)
29 ReDim vLWRs(0 To rng.Cells.Count - 1)
'~~> Change number format to Text
30 .NumberFormat = "@"
31 On Error GoTo qSkip2
32 vLWRs(0) = VBA.String$(NumOfDigits - Len(rng), strText) & rng.Value
'~~> Pass it back to the cells
33 rng = vLWRs
qSkip2:
34 End If
35 End With
36 End With
37 iCount = rng.Rows.Count
'~~> Code Ends
BeforeExit:
'~~> Remove items from memory
38 Set rng = Nothing
'~~> Speeding Up VBA Code
39 Call SpeedUp(True)
40 If bErrorHandle = False Then
'~~> No Errors
41 MsgBox "No of Records - " & Format(iCount, "#,##0;[Red](#,##0);-_)") & DblSpace _
& "Time taken: " & Format(Now() - aStartTime, "h:mm:ss") & DblSpace _
& " You're good to go!" & DblSpace & _
CoName & Chr(32) & Chr(169) & Chr(32) & Year(Date), vbInformation, "Excellent"
42 End If
43 Exit Sub
'~~> Error Occurred
errhandler:
44 bErrorHandle = True
45 ProcName = Application.VBE.ActiveCodePane.CodeModule.ProcOfLine(Application.VBE.ActiveCodePane.TopLine, 0)
46 MsgBox IIf(Erl = 0, "", "Error on line " & Erl) & vbNewLine & "Procedure: - " & ProcName & vbNewLine & "Error " & Err.Number & " " & Err.Description, vbCritical, "Oops I did it again...."
'MsgBox "Procedure: - " & ProcName & DblSpace & Err.Description, vbCritical, "Oops I did it again...."
47 Resume BeforeExit
End Sub
'#### SpeedUp (False) - Speeds the VBA Code #####
'#### SpeedUp (True) - Slows down the VBA Code ####
Public Function SpeedUp(Optional bSpeed As Boolean = True)
With Application
.ScreenUpdating = bSpeed 'Prevent screen flickering
'.Calculation = IIf(bSpeed, xlAutomatic, xlCalculationManual) 'Preventing calculation
.DisplayAlerts = bSpeed 'Turn OFF alerts
.EnableEvents = bSpeed 'Prevent All Events
.Cursor = IIf(bSpeed, xlDefault, xlWait) 'Prevent Hour Glass
.StatusBar = IIf(bSpeed, vbNullString, "Please wait...")
'.Application.Interactive = bSpeed 'Block all input from the keyboard and mouse
End With
End Function