Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default How to preserve leading zeroes

I have an Access database that via VB oode pumps data into a spreadsheet.
Some of the data looks like numbers, but in fact are billing codes, etc.
that need to be treated as text, not as numerics, so that the leading 0s are
preserved, e.g., "001765" should appear, not "1765". For the life of me I
cannot find a simple answer to this seemingly small problem. I have tried
everything I can find in the Excel/VB documentation and I always wind up with
"1765". If I had any hair to begin with, I would have torn it all out by
now. {Full tirade that would appear here has been omitted to preserve
space.} Any suggestions? Thanks!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How to preserve leading zeroes


If the numbers all have the same length, you can try the following whic
works on one cell. With a minor bit of modification you can make i
work on all of the returned data using a loop.

Range("A1").Select
Selection.NumberFormat = "@"
Dim I As Variant
I = Range("A1").Value
Select Case Len(I)
Case Is = 1: I = "00000" & I
Case Is = 2: I = "0000" & I
Case Is = 3: I = "000" & I
Case Is = 4: I = "00" & I
Case Is = 5: I = "0" & I
Case Else: I = I
End Select
Range("A1") =

--
Lauri
-----------------------------------------------------------------------
Laurin's Profile: http://www.excelforum.com/member.php...fo&userid=2683
View this thread: http://www.excelforum.com/showthread.php?threadid=49185

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default How to preserve leading zeroes

Laurin:
Thank you very much. I will give it a try. Does this not seem like an
awful lot of trouble, though, to fix something that should be quite simple?
When you are in the spreadsheet, you can just go to Format Cells, select
Text, and then type in all the leading 0s you want. I'm rather astounded
that something I can do so easily in the interface seems programmatically to
require a detour through Siberia.
Peter
"Laurin" wrote:


If the numbers all have the same length, you can try the following which
works on one cell. With a minor bit of modification you can make it
work on all of the returned data using a loop.

Range("A1").Select
Selection.NumberFormat = "@"
Dim I As Variant
I = Range("A1").Value
Select Case Len(I)
Case Is = 1: I = "00000" & I
Case Is = 2: I = "0000" & I
Case Is = 3: I = "000" & I
Case Is = 4: I = "00" & I
Case Is = 5: I = "0" & I
Case Else: I = I
End Select
Range("A1") = I


--
Laurin
------------------------------------------------------------------------
Laurin's Profile: http://www.excelforum.com/member.php...o&userid=26832
View this thread: http://www.excelforum.com/showthread...hreadid=491854


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default How to preserve leading zeroes

pbrase wrote:

I have an Access database that via VB oode pumps data into a spreadsheet.
Some of the data looks like numbers, but in fact are billing codes, etc.
that need to be treated as text, not as numerics, so that the leading 0s are
preserved, e.g., "001765" should appear, not "1765". For the life of me I
cannot find a simple answer to this seemingly small problem. I have tried
everything I can find in the Excel/VB documentation and I always wind up with
"1765". If I had any hair to begin with, I would have torn it all out by
now. {Full tirade that would appear here has been omitted to preserve
space.} Any suggestions? Thanks!


Have you tried making VB enter '001765?


--
Gordon Rainsford

London UK
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How to preserve leading zeroes


Laurin:
Thank you very much. I will give it a try. Does this not seem like an
awful lot of trouble, though, to fix something that should be quit
simple?
When you are in the spreadsheet, you can just go to Format Cells
select
Text, and then type in all the leading 0s you want. I'm rathe
astounded
that something I can do so easily in the interface seem
programmatically to
require a detour through Siberia.
Peter


I agree that it does. I'm not very experienced in programming so ther
may be an easier way.

For the loop do this. First select the colum that you want to forma
and name the range Data. Then use the following:


Code
-------------------
Option Base 1

Sub Formatting()

Dim v as variant, Output() as string, I as Long
[Data].NumberFormat="@"
v = [Data]
Redim Output(ubound(v,1))
For I = 1 to ubound(v,1)
Select Case Len(v(I,1))
Case Is = 1: Output(I) = "00000" & v(I,1)
Case Is = 2: Output(I) = "0000" & v(I,1)
Case Is = 3: Output(I) = "000" & v(I,1)
Case Is = 4: Output(I) = "00" & v(I,1)
Case Is = 5: Output(I) = "0" & v(I,1)
Case Else: Output(I) = v(I,1)
End Select
Next I

[Data] = Application.worksheetfunction.transpose(Output)

End Su
-------------------

--
Lauri
-----------------------------------------------------------------------
Laurin's Profile: http://www.excelforum.com/member.php...fo&userid=2683
View this thread: http://www.excelforum.com/showthread.php?threadid=49185



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default How to preserve leading zeroes

Incidentally, Laurin, you can probably get to the same place, instead of
using the Case statements, doing something like this:
Output = Right("000000" & InputValue, 6)
All the values are 6 digits long, so in this way you would get the string in
the right length with the appropriate number of 0s prepended. I use this
type of techique all the time in my Access code.

"Laurin" wrote:


Laurin:
Thank you very much. I will give it a try. Does this not seem like an
awful lot of trouble, though, to fix something that should be quite
simple?
When you are in the spreadsheet, you can just go to Format Cells,
select
Text, and then type in all the leading 0s you want. I'm rather
astounded
that something I can do so easily in the interface seems
programmatically to
require a detour through Siberia.
Peter


I agree that it does. I'm not very experienced in programming so there
may be an easier way.

For the loop do this. First select the colum that you want to format
and name the range Data. Then use the following:


Code:
--------------------
Option Base 1

Sub Formatting()

Dim v as variant, Output() as string, I as Long
[Data].NumberFormat="@"
v = [Data]
Redim Output(ubound(v,1))
For I = 1 to ubound(v,1)
Select Case Len(v(I,1))
Case Is = 1: Output(I) = "00000" & v(I,1)
Case Is = 2: Output(I) = "0000" & v(I,1)
Case Is = 3: Output(I) = "000" & v(I,1)
Case Is = 4: Output(I) = "00" & v(I,1)
Case Is = 5: Output(I) = "0" & v(I,1)
Case Else: Output(I) = v(I,1)
End Select
Next I

[Data] = Application.worksheetfunction.transpose(Output)

End Sub
--------------------


--
Laurin
------------------------------------------------------------------------
Laurin's Profile: http://www.excelforum.com/member.php...o&userid=26832
View this thread: http://www.excelforum.com/showthread...hreadid=491854


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
Leading zeroes formula Steve[_4_] Excel Worksheet Functions 3 March 18th 09 06:57 PM
Sorry, leading zeroes again :-(( Argghhhh Excel Discussion (Misc queries) 7 November 13th 06 10:26 PM
Leading Zeroes Melissa Excel Discussion (Misc queries) 3 October 11th 06 07:38 PM
Leading Zeroes Ken Excel Discussion (Misc queries) 1 October 7th 05 03:17 PM
CSV leading zeroes Tim_nol Excel Discussion (Misc queries) 2 December 28th 04 08:19 PM


All times are GMT +1. The time now is 06:23 PM.

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

About Us

"It's about Microsoft Excel"