Thread: Help with Macro
View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Help with Macro

Thanks for point that out

Sub HypenateB()
Dim rng as Range, cell as Range
On Error Resume Next
set rng = Columns(2).SpecialCells(xlConstants,xlTextValues)
On Error goto 0
if not rng is nothing then
for each cell in rng
cell.Value = Left(trim(cell),2) & "-" & _
right(Trim(cell),len(trim(cell))-2)
Next
End if
End Sub

or

Sub HypenateB()
Dim rng as Range, cell as Range
On Error Resume Next
set rng = Columns(2).SpecialCells(xlConstants,xlTextValues)
On Error goto 0
if not rng is nothing then
for each cell in rng
sCell = Trim(cell)
cell.Value = Left(scell,2) & "-" & _
right(scell,len(scell)-2)
Next
End if
End Sub

--
Regards,
Tom Ogilvy


steve wrote in message
...
Tom,

You solution catches the spaces at the beginning of the string
" 131212A"
and results in
" - 131212A"

(see my use of Trim)

--
sb
"Tom Ogilvy" wrote in message
...
Sub HypenateB()
Dim rng as Range, cell as Range
On Error Resume Next
set rng = Columns(2).SpecialCells(xlConstants,xlTextValues)
On Error goto 0
if not rng is nothing then
for each cell in rng
cell.Value = Left(cell,2) & "-" & right(cell,len(cell)-2)
Next
End if
End Sub

Test it on a copy of your data.
--
Regards,
Tom Ogilvy


"Chad Holstead" wrote in message
...
I am not very good at VB and need some help with a
Macro. I have spread sheet that is exported from another
program. We need to reformat the data in Column B. The
data comes into excell like this " 131212A" It needs
to look like this "13-1212A". I have tried the standard
recored macro, however it just replaces the data with the
hard coded value. I need this to be able to handle
diffrent values in column B.

Any ideas would really help, I think this easy, I'm just
not a vb guy.

Thans
Chad