ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   PADDING STRINGS !!!!! (https://www.excelbanter.com/excel-programming/441387-padding-strings.html)

Jay Dean

PADDING STRINGS !!!!!
 
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 ***

Dave Peterson

PADDING STRINGS !!!!!
 
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


jay dean wrote:

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 ***


--

Dave Peterson

Jay Dean

PADDING STRINGS !!!!!
 
Thanks Dave, and Blue Max !
That's exactly what I wanted.

Jay Dean



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


All times are GMT +1. The time now is 10:00 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com