PADDING STRINGS !!!!!

J

jay dean

Hello -

Arr1() contains strings. When I unload them into my sheet (one index
Arr1()content per cell), I would like for all the single digits to be
padded with a zero. For example, "1" should show as "01", "5" as "05",
e.t.c.. Any help would be appreciated.

Thanks
Jay Dean

*** Sent via Developersdex http://www.developersdex.com ***
 
B

Blue Max

Hello Jay,

One thought is to simply change the number format for the desired cells.
For example, create a custom number format that simply consists of "00" or
"00.00". The format will pad anything less than two digits to the left of
the decimal with a zero. For example, the first format "00" will display
numbers as:

07
08
09
10
11
12

I believe that this is what you wanted, correct?

Good Luck,

Richard

****************************
 
D

Dave Peterson

You could keep the values numeric and format the cell with a custom format: 00

Or you could format the cells as Text and plop them in as text.

Option Explicit
Sub testme01()

Dim myArr As Variant
Dim myCell As Range

myArr = Array(1, 2, 12, 25)

Set myCell = ActiveSheet.Range("A1")

With myCell.Resize(UBound(myArr) - LBound(myArr) + 1, 1)
.NumberFormat = "00"
.Value = Application.Transpose(myArr)
End With

End Sub

or

Option Explicit
Sub testme02()

Dim myArr As Variant
Dim myCell As Range
Dim iCtr As Long
Dim myOffset As Long

myArr = Array(1, 2, 12, 25)

Set myCell = ActiveSheet.Range("A1")

With myCell.Resize(UBound(myArr) - LBound(myArr) + 1, 1)
.NumberFormat = "@" 'text
End With

myOffset = 0
For iCtr = LBound(myArr) To UBound(myArr)
myCell.Offset(myOffset, 0).Value = Format(myArr(iCtr), "00")
myOffset = myOffset + 1
Next iCtr

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top