Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Macro to insert "0" in front of 4 character string

I need to write a small macro that will accomplish the
following. I am working with a column of numbers (as text)
they are zip codes. The zipcodes that begin with zero have
had their starting zero removed by the program in which
they were initially stored. I need to insert a zero in
front of any zipcode that has only 4 characters. Here is
what I am currently doing in an active cell like [2345]

[F2] to go into edit mode
[Home] to move to the front of the string
[0] to insert the "0" at the front of the string
[Enter] to go down to the next cell.

I want to create a keystroke macro that is faster

Any ideas.

Thanks Lowell
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,337
Default Macro to insert "0" in front of 4 character string

Just format the selection as TEXT and use this
Sub insertzero()
For Each c In Selection
If Len(c) = 4 Then c.Value = "0" & c
Next
End Sub

"Lowell B. Copeland" wrote in message
...
I need to write a small macro that will accomplish the
following. I am working with a column of numbers (as text)
they are zip codes. The zipcodes that begin with zero have
had their starting zero removed by the program in which
they were initially stored. I need to insert a zero in
front of any zipcode that has only 4 characters. Here is
what I am currently doing in an active cell like [2345]

[F2] to go into edit mode
[Home] to move to the front of the string
[0] to insert the "0" at the front of the string
[Enter] to go down to the next cell.

I want to create a keystroke macro that is faster

Any ideas.

Thanks Lowell



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Macro to insert "0" in front of 4 character string

Sub Addzero()
Dim cell As Range
Selection.NumberFormat = "@"
For Each cell In Selection
cell.Value = Right("000000" & cell.Value, 5)
Next
End Sub


worked for me. Select the range of cells containing zip codes (it won't
hurt the ones with 5 numbers) and run the macro
This assumes only 5 digit zipcodes - if you have 9 digit zipcodes this won't
work.

--
Regards,
Tom Ogilvy


"Lowell B. Copeland" wrote in message
...
I need to write a small macro that will accomplish the
following. I am working with a column of numbers (as text)
they are zip codes. The zipcodes that begin with zero have
had their starting zero removed by the program in which
they were initially stored. I need to insert a zero in
front of any zipcode that has only 4 characters. Here is
what I am currently doing in an active cell like [2345]

[F2] to go into edit mode
[Home] to move to the front of the string
[0] to insert the "0" at the front of the string
[Enter] to go down to the next cell.

I want to create a keystroke macro that is faster

Any ideas.

Thanks Lowell



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
Lost "File Menu" - now it's "Edit / View / Insert.." but no "F daves Excel Discussion (Misc queries) 3 April 24th 07 04:52 AM
How can I convert a character string that contains letters & numbers "£2,456.99 (including flights)" into a numeric? ship Excel Worksheet Functions 4 April 3rd 07 02:35 PM
insert "-" as fourth character in number string cursednomore Excel Discussion (Misc queries) 2 February 7th 07 07:33 PM
Does the cell contain the character "-" within the string? Eric Excel Discussion (Misc queries) 4 December 3rd 06 03:46 PM
Insert "-" in text "1234567890" to have a output like this"123-456-7890" Alwyn Excel Discussion (Misc queries) 3 October 25th 05 11:36 PM


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

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"