Replace characters in String if not equal to Arrays

spidaman

Board Regular
Joined
Jul 26, 2015
Messages
116
Office Version
  1. 365
Platform
  1. Windows
Can anyone help with this bit of code please?

With a string entered into a userform textbox, I'd like to replace any character that doesn't match one of the characters in 2 x arrays:

VBA Code:
Private Sub cmdRun_Click()

Dim Target As Variant
Dim Targarray1(0 - 9) As Integer
Dim Targarray2("+", "-", "(", ")", " ") As String

Target = txtbox.Value

For Each i In Len(Target)
    If i <> Targarray1 Or i <> Targarray2 Then
        Replace(Target,i,"")
    End If
Next i

End Sub

There are some errors with the Replace function and probably with the Arrays as well?

Thanks in advance.
 
Give this a try then...
VBA Code:
Private Sub cmdRun_Click()
  Dim X As Long, Txt As String
  Txt = txtbox.Text
  For X = 1 To Len(Txt)
    If Not Mid(Txt, X, 1) Like "[0-9()+-]" Then Mid(Txt, X) = " "
  Next
  txtbox.Text = Replace(Txt, " ", "")
End Sub
That's perfect Rick, thanks very much.
 
Upvote 0

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
A regex version.

VBA Code:
Sub cmdRun_Click()

With CreateObject("VBScript.RegExp")
    .Pattern = "([0-9\(\)\+\-\s])"
    .Global = True
    txtbox.Value = .Replace(txtbox.Value, "")
End With

End Sub
Thanks lrobbo314 I'll test this option as well tomorrow with the adjustment from Peter_SSs
 
Upvote 0
That's perfect Rick, thanks very much.

That version of Rick removes the blank spaces " ", if you need them, try the following:

VBA Code:
Private Sub cmdRun_Click()
  Dim i As Long, Txt As String, c As String
 
  Txt = txtbox.Text
  With txtbox
    For i = 1 To Len(Txt)
      If Mid(Txt, i, 1) Like "[0-9()+-]" Or Mid(Txt, i, 1) = " " Then c = c & Mid(Txt, i, 1)
    Next
  End With
  txtbox.Text = c
End Sub
 
Upvote 0
That version of Rick removes the blank spaces " ", if you need them, try the following:

VBA Code:
Private Sub cmdRun_Click()
  Dim i As Long, Txt As String, c As String

  Txt = txtbox.Text
  With txtbox
    For i = 1 To Len(Txt)
      If Mid(Txt, i, 1) Like "[0-9()+-]" Or Mid(Txt, i, 1) = " " Then c = c & Mid(Txt, i, 1)
    Next
  End With
  txtbox.Text = c
End Sub
Thanks for the option DanteAmor
 
Upvote 0
I carried the removal of the space character over from my first interpretation of the OP's request (remove, not keep, the indicated characters). Leaving the space in with the code I posted is easy, just change this line of code...

For Each Char In Split("0,1,2,3,4,5,6,7,8,9,+,-,(,), ", ",")

to this....

For Each Char In Split("0,1,2,3,4,5,6,7,8,9,+,-,(,)", ",")

All I did is remove the space comma/space at the end of the first quoted text.
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,665
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top