Originally Posted by
Zack Barresse
If you're entering 07885 into a cell, and it's formatted as General or any numeric, it will lop off leading zero's. So you'd either need to use the Format() function as Kenneth suggested, set the cell format to text and then add your leading zero's, or set the value to a string variable at run-time and add the leading zero's as desired.
Your code uses two different variable types, where the workingVar is a string, so it's value will be literal, while you're also passing the Value of a cell, which means whatever you see in the formula bar, and looks like you're passing it as a Range variable.
Hmm...I understand what youre saying, but I feel like i'm literally assigning a variable a value and having it come up with a different variable.
What can I change out of this block of code to make thecell.value actually equal the workingvar? [in this case, my output in the immediate window is..
Adding...07885
Cell Value SHOULD BE = 07885
Cell Value IS = 7885
Final Cell is...7885
and my code is...
...
Call lengthCheck(cellVar)
Debug.Print "Final Cell is..." & cellVar.Value
...
Sub lengthCheck(theCell As Range)
Dim splitter() As String
Dim workingVar As String
Dim j As Integer
splitter = Split(theCell, "-") 'get rid of -
workingVar = splitter(LBound(splitter))
workingVar = Application.WorksheetFunction.Clean(workingVar) 'remove special chars
If Len(workingVar) < 5 Then
For j = 1 To (5 - Len(workingVar)) 'for however many 0's its missing, add one
workingVar = 0 & workingVar
Debug.Print "Adding..." & workingVar
Next
End If
If Len(workingVar) > 5 Then
workingVar = Left(workingVar, 5) 'only take the left 5 digits of a long zip
Debug.Print "Trimming..." & workingVar
End If
theCell.Value = workingVar
Debug.Print "Cell Value SHOULD BE = " & workingVar
Debug.Print "Cell Value IS = " & theCell.Value
End Sub