Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 41
Default VBA inserting a space in a text

Hi,
I need to create a macro to convert a canadian postal code.
The user received different files with the postal code in a wrong format.
The data are not always in the same column.
I just want the user to select the column or the range then click the macro
to convert it in the right format.
Format they received: h2r4d5
After they click the macro I need to see H2R 4D5 (space after the third
digit)
Normaly I do this using another column with the formula =left(a1,3)&"
"&right(a1,3)
Then copie the value to replace the data.
It will be faster if we can do this in VBA and the userid doesn't need to
know how to type the formula.
Thank You
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,058
Default VBA inserting a space in a text

This will look familiar:


Sub go_postal()
Dim r As Range, v As String
For Each r In Selection
v = r.Value
r.Value = Left(v, 3) & " " & Right(v, 3)
Next
End Sub

Just select the cells (or columns) and run the macro. VBA has the advantage
that you do not need a helper column.
--
Gary's Student


"Mouimet" wrote:

Hi,
I need to create a macro to convert a canadian postal code.
The user received different files with the postal code in a wrong format.
The data are not always in the same column.
I just want the user to select the column or the range then click the macro
to convert it in the right format.
Format they received: h2r4d5
After they click the macro I need to see H2R 4D5 (space after the third
digit)
Normaly I do this using another column with the formula =left(a1,3)&"
"&right(a1,3)
Then copie the value to replace the data.
It will be faster if we can do this in VBA and the userid doesn't need to
know how to type the formula.
Thank You

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,651
Default VBA inserting a space in a text

On Thu, 9 Nov 2006 08:39:02 -0800, Mouimet
wrote:

Hi,
I need to create a macro to convert a canadian postal code.
The user received different files with the postal code in a wrong format.
The data are not always in the same column.
I just want the user to select the column or the range then click the macro
to convert it in the right format.
Format they received: h2r4d5
After they click the macro I need to see H2R 4D5 (space after the third
digit)
Normaly I do this using another column with the formula =left(a1,3)&"
"&right(a1,3)
Then copie the value to replace the data.
It will be faster if we can do this in VBA and the userid doesn't need to
know how to type the formula.
Thank You


Perhaps something like:

==================================
Option Explicit
Sub FormatPostCode()
Dim c As Range
Dim i As Long
Dim sCode As String

For Each c In Selection
sCode = Replace(c.Text, " ", "")
sCode = UCase(sCode)

'check for valid post code
If Len(sCode) < 6 Then Exit Sub
'or display some error message

sCode = Left(sCode, 3) & " " & Right(sCode, 3)
c.Value = sCode
Next c
End Sub
==========================
--ron
  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 41
Default VBA inserting a space in a text

Thank you very much,
This is exacly what I need.
Have a nice day

"Gary''s Student" wrote:

This will look familiar:


Sub go_postal()
Dim r As Range, v As String
For Each r In Selection
v = r.Value
r.Value = Left(v, 3) & " " & Right(v, 3)
Next
End Sub

Just select the cells (or columns) and run the macro. VBA has the advantage
that you do not need a helper column.
--
Gary's Student


"Mouimet" wrote:

Hi,
I need to create a macro to convert a canadian postal code.
The user received different files with the postal code in a wrong format.
The data are not always in the same column.
I just want the user to select the column or the range then click the macro
to convert it in the right format.
Format they received: h2r4d5
After they click the macro I need to see H2R 4D5 (space after the third
digit)
Normaly I do this using another column with the formula =left(a1,3)&"
"&right(a1,3)
Then copie the value to replace the data.
It will be faster if we can do this in VBA and the userid doesn't need to
know how to type the formula.
Thank You

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
How do I remove a space infront of text in a cell? GAC Excel Discussion (Misc queries) 7 October 10th 06 01:41 AM
removing a space from starting of the text shikha Excel Worksheet Functions 3 September 24th 05 11:16 PM
Inserting a Space in The Same Place Formula carl Excel Worksheet Functions 2 March 4th 05 11:10 PM
space between text strings with concatenate Jeff Excel Discussion (Misc queries) 2 March 3rd 05 06:54 PM
Read Text File into Excel Using VBA Willie T Excel Discussion (Misc queries) 13 January 8th 05 12:37 AM


All times are GMT +1. The time now is 02:44 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"