Worksheet_change error

G

Guest

Hi All
I have a great code that replaces characters with symbols - and it works
brilliantly, until I select multiple cells on the worksheet and hit the
delete key.
I then get a "Run-time error '13': Type mismatch" error message.
Can someone tell me what I need to add to this code to stop the error if a
user hits the delete key?

Private Sub Worksheet_Change(ByVal Target As Range)
Set r = Target
Dim s As String
s = r.Value
s = Replace(s, "[", ChrW(780))
s = Replace(s, "]", ChrW(768))
s = Replace(s, "<", ChrW(772))
s = Replace(s, ">", ChrW(769))
s = Replace(s, "~", ChrW(776))
Application.EnableEvents = False
r.Value = s
Application.EnableEvents = True
End Sub
 
G

Guest

Try:-

Private Sub Worksheet_Change(ByVal target As Range)
Application.EnableEvents = False
Dim s As String
Set myRange = target
For Each r In myRange
s = r.Value
s = Replace(s, "[", ChrW(780))
s = Replace(s, "]", ChrW(768))
s = Replace(s, "<", ChrW(772))
s = Replace(s, ">", ChrW(769))
s = Replace(s, "~", ChrW(776))
r.Value = s
Next
Application.EnableEvents = True
End Sub

Mike
 
G

Guest

Fantastic - thanks heaps Mike!!!!
--
BeSmart


Mike H said:
Try:-

Private Sub Worksheet_Change(ByVal target As Range)
Application.EnableEvents = False
Dim s As String
Set myRange = target
For Each r In myRange
s = r.Value
s = Replace(s, "[", ChrW(780))
s = Replace(s, "]", ChrW(768))
s = Replace(s, "<", ChrW(772))
s = Replace(s, ">", ChrW(769))
s = Replace(s, "~", ChrW(776))
r.Value = s
Next
Application.EnableEvents = True
End Sub

Mike


BeSmart said:
Hi All
I have a great code that replaces characters with symbols - and it works
brilliantly, until I select multiple cells on the worksheet and hit the
delete key.
I then get a "Run-time error '13': Type mismatch" error message.
Can someone tell me what I need to add to this code to stop the error if a
user hits the delete key?

Private Sub Worksheet_Change(ByVal Target As Range)
Set r = Target
Dim s As String
s = r.Value
s = Replace(s, "[", ChrW(780))
s = Replace(s, "]", ChrW(768))
s = Replace(s, "<", ChrW(772))
s = Replace(s, ">", ChrW(769))
s = Replace(s, "~", ChrW(776))
Application.EnableEvents = False
r.Value = s
Application.EnableEvents = True
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top