Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
a a is offline
external usenet poster
 
Posts: 51
Default Preserving leading zero

Hello,

I'm certain that there should be a way to preserve a leading zero when
you don't know the length of the string - but I certainly don't know how.

I have a string like "010-056". If I put the variable in a watch window
- it comes up as a string. The problem is that I need to get rid of the
"-". When I do this, the string becomes a number.

Because the left character is not always a zero and, as I mentioned, the
length of the string varies, I cannot figure out how to keep the leading
zero intact when I replace the "-".

Does anybody have a solution to this?

Any help in this would be most appreciated.

Thanks much in advance.

Anita

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 44
Default Preserving leading zero

You can format the cell as text, then whatever you type, it will display that
unchanged.


--
Dave


"a" wrote:

Hello,

I'm certain that there should be a way to preserve a leading zero when
you don't know the length of the string - but I certainly don't know how.

I have a string like "010-056". If I put the variable in a watch window
- it comes up as a string. The problem is that I need to get rid of the
"-". When I do this, the string becomes a number.

Because the left character is not always a zero and, as I mentioned, the
length of the string varies, I cannot figure out how to keep the leading
zero intact when I replace the "-".

Does anybody have a solution to this?

Any help in this would be most appreciated.

Thanks much in advance.

Anita


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Preserving leading zero

This is not a problem in VBA:


Sub gsnu()
Dim s As String
s = "010-056"
MsgBox (s)
s = Replace(s, "-", "")
MsgBox (s)
End Sub

In the worksheet it is a problem. Find/Replace will convert the string into
a number and drop the leading zero.

If the data is entered as:
'010-056
rather than
010-056
then Find/Replace will leave the leading zero alone. It is also very easy
to convert existing data from text into apostrophe'd text.
--
Gary's Student


"a" wrote:

Hello,

I'm certain that there should be a way to preserve a leading zero when
you don't know the length of the string - but I certainly don't know how.

I have a string like "010-056". If I put the variable in a watch window
- it comes up as a string. The problem is that I need to get rid of the
"-". When I do this, the string becomes a number.

Because the left character is not always a zero and, as I mentioned, the
length of the string varies, I cannot figure out how to keep the leading
zero intact when I replace the "-".

Does anybody have a solution to this?

Any help in this would be most appreciated.

Thanks much in advance.

Anita


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,092
Default Preserving leading zero

You didn't mention how you got rid of the "-", but this seems to work for
me:

I have this in cell A1: 010-056

Sub test()
Dim x As String

x = Range("A1").Value
x = Replace(x, "-", "", , , vbTextCompare)
End Sub

Mike F
"a" wrote in message
link.net...
Hello,

I'm certain that there should be a way to preserve a leading zero when you
don't know the length of the string - but I certainly don't know how.

I have a string like "010-056". If I put the variable in a watch window -
it comes up as a string. The problem is that I need to get rid of the
"-". When I do this, the string becomes a number.

Because the left character is not always a zero and, as I mentioned, the
length of the string varies, I cannot figure out how to keep the leading
zero intact when I replace the "-".

Does anybody have a solution to this?

Any help in this would be most appreciated.

Thanks much in advance.

Anita



  #5   Report Post  
Posted to microsoft.public.excel.programming
a a is offline
external usenet poster
 
Posts: 51
Default Preserving leading zero

Davey,

Thanks for responding. I did format the column as text but that doesn't
seem to be enough. I've even tried using cstr but, as the text comes
from another sheet and then I replace the "-" with nothing, the cstr
won't work because of the timing. Once the "-" has been replaced, the
leading zero is gone.

For instance, the text is "003-500". It comes from a cell that is
formatted as text into another spreadsheet which is also formatted as
text. Once the "-" is replaced, it becomes a value even though the cell
formatting is still "text".

Any other thoughts?

Best Regards,
Anita

DaveyJones wrote:

You can format the cell as text, then whatever you type, it will display that
unchanged.





  #6   Report Post  
Posted to microsoft.public.excel.programming
a a is offline
external usenet poster
 
Posts: 51
Default Preserving leading zero

Gary's Student,

This looks like it should work but when I try it, I get the message "sub
or function not defined". If I do a debug - it points to "replace". Is
there something I have to do to get VBA to recogonize "Replace"?

Thanks to you, Davey Jones and Mike Fogelman for you help.

Regards,
Anita

Gary''s Student wrote:

This is not a problem in VBA:


Sub gsnu()
Dim s As String
s = "010-056"
MsgBox (s)
s = Replace(s, "-", "")
MsgBox (s)
End Sub

In the worksheet it is a problem. Find/Replace will convert the string into
a number and drop the leading zero.

If the data is entered as:
'010-056
rather than
010-056
then Find/Replace will leave the leading zero alone. It is also very easy
to convert existing data from text into apostrophe'd text.


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 44
Default Preserving leading zero

Yeah, sorry. Never used replace with txt formatting. Use a custom format with
000000 as the format type. Think that'd do it...
--
Dave


"a" wrote:

Davey,

Thanks for responding. I did format the column as text but that doesn't
seem to be enough. I've even tried using cstr but, as the text comes
from another sheet and then I replace the "-" with nothing, the cstr
won't work because of the timing. Once the "-" has been replaced, the
leading zero is gone.

For instance, the text is "003-500". It comes from a cell that is
formatted as text into another spreadsheet which is also formatted as
text. Once the "-" is replaced, it becomes a value even though the cell
formatting is still "text".

Any other thoughts?

Best Regards,
Anita

DaveyJones wrote:

You can format the cell as text, then whatever you type, it will display that
unchanged.




  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Preserving leading zero

I m sorry. REPLACE was introduced in Excel 2000. If you don't have REPLACE
then we can use the older form:


Sub gsnu2()
Dim s As String
s = "010-056"
MsgBox (s)
s = Application.Substitute(s, "-", "")
MsgBox (s)
End Sub
--
Gary''s Student


"a" wrote:

Gary's Student,

This looks like it should work but when I try it, I get the message "sub
or function not defined". If I do a debug - it points to "replace". Is
there something I have to do to get VBA to recogonize "Replace"?

Thanks to you, Davey Jones and Mike Fogelman for you help.

Regards,
Anita

Gary''s Student wrote:

This is not a problem in VBA:


Sub gsnu()
Dim s As String
s = "010-056"
MsgBox (s)
s = Replace(s, "-", "")
MsgBox (s)
End Sub

In the worksheet it is a problem. Find/Replace will convert the string into
a number and drop the leading zero.

If the data is entered as:
'010-056
rather than
010-056
then Find/Replace will leave the leading zero alone. It is also very easy
to convert existing data from text into apostrophe'd text.



  #9   Report Post  
Posted to microsoft.public.excel.programming
a a is offline
external usenet poster
 
Posts: 51
Default Preserving leading zero

Thanks Gary's Student,

That did it. I'm working in an old version of Excel at home. I do have
2000 at work and so I'll try the other syntax when I get there.

Thanks also to DaveyJones and Mike Fogleman.

You folks are great!

Best Regards,
Anita

Gary''s Student wrote:

I m sorry. REPLACE was introduced in Excel 2000. If you don't have REPLACE
then we can use the older form:


Sub gsnu2()
Dim s As String
s = "010-056"
MsgBox (s)
s = Application.Substitute(s, "-", "")
MsgBox (s)
End Sub


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
Preserving date format Julia862621 Excel Discussion (Misc queries) 1 May 11th 10 03:17 PM
Preserving customization donbowyer Setting up and Configuration of Excel 1 February 9th 06 09:52 AM
Preserving Formulas drwexcel Excel Discussion (Misc queries) 0 June 1st 05 05:23 PM
creating a text record from a number preserving the leading zero ww Excel Programming 0 September 2nd 04 07:34 PM
creating a text record from a number preserving the leading zero ww Excel Programming 1 September 2nd 04 06:22 PM


All times are GMT +1. The time now is 05:58 PM.

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"