Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 300
Default Padding formatted numbers with leading spaces

In VBA I'd like to format numbers with a fixed width. I want to pad blanks
to the front of the formatted number to get to the desired width. I know
there is a function that returns a desired number of spaces. What is it?
Then I can write a short function where I format the number, measure its
width, and then tack on the spaces needed. Or is there an easier way that
I'm missing? Like a string to give Excel's Format function that makes it
return a desired width?

Don <donwiss at panix.com.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Padding formatted numbers with leading spaces

If you know how many spaces, just format it that way to begin with:
dim myStr as string
mystr = format(range("a1").value,"0000000000")
will give me up to 10 (if I counted correctly) leading zeros.

FYI:
There's a Space function for repeating the space character:
msgbox "hi" & space(12) & "there"

There's a string function to string a bunch of characters:
msgbox string(12, "a")
even 0's
msgbox string(12,"0")

So this may be easier to decipher later:

mystr = format(range("a1").value,string(10,"0"))




Don Wiss wrote:

In VBA I'd like to format numbers with a fixed width. I want to pad blanks
to the front of the formatted number to get to the desired width. I know
there is a function that returns a desired number of spaces. What is it?
Then I can write a short function where I format the number, measure its
width, and then tack on the spaces needed. Or is there an easier way that
I'm missing? Like a string to give Excel's Format function that makes it
return a desired width?

Don <donwiss at panix.com.


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 300
Default Padding formatted numbers with leading spaces

On Mon, 10 Jan 2005, Dave Peterson wrote:

If you know how many spaces, just format it that way to begin with:
dim myStr as string
mystr = format(range("a1").value,"0000000000")
will give me up to 10 (if I counted correctly) leading zeros.


But I want leading spaces, not leading zeros. Then when I concatenate the
strings together I can get the numbers in the columns to line up. This is
what I came up with:

Function FmtPHG(N As Double) As String
' format hard coded to use with payroll by HG report
' arg: N - number to format

Dim S As String

If N = 0 Then
S = Space(8) & "- "
Else
S = Format(N, "##0.0%")
S = Space(10 - Len(S)) & S
End If

FmtPHG = S

End Function

Thanks for the Space() function.

Don <donwiss at panix.com.
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Padding formatted numbers with leading spaces

ahhh. I missed that (in your subject and in the body!)

S = Format(N, "0.0%")
s = right(space(10) & s, 10)

(or some variation) might work ok, too.





Don Wiss wrote:

On Mon, 10 Jan 2005, Dave Peterson wrote:

If you know how many spaces, just format it that way to begin with:
dim myStr as string
mystr = format(range("a1").value,"0000000000")
will give me up to 10 (if I counted correctly) leading zeros.


But I want leading spaces, not leading zeros. Then when I concatenate the
strings together I can get the numbers in the columns to line up. This is
what I came up with:

Function FmtPHG(N As Double) As String
' format hard coded to use with payroll by HG report
' arg: N - number to format

Dim S As String

If N = 0 Then
S = Space(8) & "- "
Else
S = Format(N, "##0.0%")
S = Space(10 - Len(S)) & S
End If

FmtPHG = S

End Function

Thanks for the Space() function.

Don <donwiss at panix.com.


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 42
Default Padding formatted numbers with leading spaces

If you want to hard-code the range, you can select the range
click: FormatCellsNumberCustom
In the Type: textbox enter 0 for the number of digits you want

Example:
0000000 in the Type box
will make 35 display as 0000035

to do this in VBA try the above with your recorder turned on.

"Don Wiss" wrote:

In VBA I'd like to format numbers with a fixed width. I want to pad blanks
to the front of the formatted number to get to the desired width. I know
there is a function that returns a desired number of spaces. What is it?
Then I can write a short function where I format the number, measure its
width, and then tack on the spaces needed. Or is there an easier way that
I'm missing? Like a string to give Excel's Format function that makes it
return a desired width?

Don <donwiss at panix.com.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 300
Default Padding formatted numbers with leading spaces

On Mon, 10 Jan 2005, gocush wrote:

Don Wiss wrote:

In VBA I'd like to format numbers with a fixed width. I want to pad blanks
to the front of the formatted number to get to the desired width. I know
there is a function that returns a desired number of spaces. What is it?
Then I can write a short function where I format the number, measure its
width, and then tack on the spaces needed. Or is there an easier way that
I'm missing? Like a string to give Excel's Format function that makes it
return a desired width?


If you want to hard-code the range, you can select the range
click: FormatCellsNumberCustom
In the Type: textbox enter 0 for the number of digits you want

Example:
0000000 in the Type box
will make 35 display as 0000035

to do this in VBA try the above with your recorder turned on.


Except my question was about padding with spaces, not zeros.

Don <donwiss at panix.com.
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Padding formatted numbers with leading spaces

Your choice:

? "==" & space(10) & "<==="
== <===
? "==" & String(10," ") & "<==="
== <===


--
Regards,
Tom Ogilvy

"Don Wiss" wrote in message
...
On Mon, 10 Jan 2005, gocush wrote:

Don Wiss wrote:

In VBA I'd like to format numbers with a fixed width. I want to pad

blanks
to the front of the formatted number to get to the desired width. I

know
there is a function that returns a desired number of spaces. What is

it?
Then I can write a short function where I format the number, measure

its
width, and then tack on the spaces needed. Or is there an easier way

that
I'm missing? Like a string to give Excel's Format function that makes

it
return a desired width?


If you want to hard-code the range, you can select the range
click: FormatCellsNumberCustom
In the Type: textbox enter 0 for the number of digits you want

Example:
0000000 in the Type box
will make 35 display as 0000035

to do this in VBA try the above with your recorder turned on.


Except my question was about padding with spaces, not zeros.

Don <donwiss at panix.com.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Padding a text column with leading zeros Chris Excel Discussion (Misc queries) 6 October 29th 07 10:45 PM
Padding with leading zeros GKW in GA Excel Discussion (Misc queries) 2 July 4th 07 12:42 AM
how do I remove leading spaces and leave the remianing spaces w Debi Excel Worksheet Functions 6 February 28th 07 03:29 PM
Padding Numbers couger77 Excel Programming 14 December 30th 03 04:02 PM
Padding out reference numbers Amanda[_6_] Excel Programming 6 December 22nd 03 10:34 AM


All times are GMT +1. The time now is 01:43 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"