Find and replace "?" character

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I have an easy question which I could solve so far.
I would like to find all ? character on a worksheet and delete it from each
cells.

My code is:

Private Sub CommandButton2_Click()
Dim rng1 As Range
Dim mycell1 As Range
Set rng1 = Range("h98:i100")
For Each mycell1 In rng1

If Not mycell1.Find("?") Is Nothing Then
mycell1.Replace What:="?", replacement:=""
End If
Next

End Sub


the result is, that it deletes each character, not just the ?s. My idea is,
that this is because ? is a special character to be used to look for
something which is not known in an expression.

How can I replace my ?s ? And onyl the ?s ?

Thanks for your help,
Zoltan
 
Enclose the ? in brackets:
Private Sub CommandButton2_Click()
Dim rng1 As Range
Dim mycell1 As Range
Set rng1 = Range("h98:i100")
For Each mycell1 In rng1

If Not mycell1.Find([?]) Is Nothing Then
mycell1.Replace What:=[], replacement:=""
End If
Next
End Sub

Et
 
Put a tilde (~) character before the ?. You can also don't need to loop.

Range("h98:i100").Replace What:="~?", Replacement:=""
 
Enclose the ? in brackets:
Private Sub CommandButton2_Click()
Dim rng1 As Range
Dim mycell1 As Range
Set rng1 = Range("h98:i100")
For Each mycell1 In rng1

If Not mycell1.Find([?]) Is Nothing Then
mycell1.Replace What:=[], replacement:=""
End If
Next
End Sub

Et

--
If this posting was helpful, please click on the Yes button.
Regards,

Michael Arch.



Zoltan said:
I have an easy question which I could solve so far.
I would like to find all ? character on a worksheet and delete it from each
cells.
My code is:
Private Sub CommandButton2_Click()
Dim rng1 As Range
Dim mycell1 As Range
Set rng1 = Range("h98:i100")
For Each mycell1 In rng1
If Not mycell1.Find("?") Is Nothing Then
mycell1.Replace What:="?", replacement:=""
End If
Next
the result is, that it deletes each character, not just the ?s. My idea is,
that this is because ? is a special character to be used to look for
something which is not known in an expression.
How can I replace my ?s ? And onyl the ?s ?
Thanks for your help,
Zoltan- Hide quoted text -

- Show quoted text -

To search for a character that is also a wildcard within a text
string, precede it with a tilde ~. That will specify the character,
not the wildcard, as the search criteria. Same thing works for find
and find/replace.
 
Back
Top