View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein \(MVP - VB\)[_1902_] Rick Rothstein \(MVP - VB\)[_1902_] is offline
external usenet poster
 
Posts: 1
Default Insert characters into String

First off, your String values appear to be 14 characters long, not 12
characters long as you stated. Assuming 14 characters is correct, give this
Workbook_Change event code a try...

Private Sub Worksheet_Change(ByVal Target As Range)
With Target
If .Column = 1 Then
If Len(Replace(.Value, "-", "")) = 14 Then
On Error GoTo Whoops
Application.EnableEvents = False
.Value = Format(Replace(.Value, "-", ""), "@@-@@@@@@@@@@@-@")
End If
End If
End With
Whoops:
Application.EnableEvents = True
End Sub

Rick


"franciz" wrote in message
...
Hi

I have string, eg. TH00000000789F in a range, let say in column A, how do
I
insert "-" after the first 2 characters and before the last character
resulting
TH-00000000789-F. The string are always 12 Characters.

Thanks in advance

regards, francis