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.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hello,

Are you after replacing ... or deleting ...?
 
Upvote 0
Hi James006

To delete any character not in either of the arrays would actually be preferable.

Thanks for your help
 
Upvote 0
How about
VBA Code:
Private Sub cmdRun_Click()
    Dim Ary As Variant
    Dim i As Long
    
    Ary = Array("+", "-", "(", ")", " ")
    For i = 0 To 9
        txtbox = Replace(txtbox, i, "")
        If i <= UBound(Ary) Then txtbox = Replace(txtbox, Ary(i), "")
    Next i
End Sub
 
Upvote 0
(Untested) I am pretty sure this code should also work...
VBA Code:
Private Sub cmdRun_Click()
  Dim Char As Variant
  For Each Char In Split("0,1,2,3,4,5,6,7,8,9,+,-,(,), ", ",")
    txtbox = Replace(txtbox, Char, "")
  Next
End Sub
 
Upvote 0
Thanks very much Fluff and Rick. I'll test both tonight when I get a chance.
 
Upvote 0
Have just checked both solutions and just realised they are both replacing 0 - 9 and "+", "-", "(", ")", " ".

Apologies for not making it clearer, but I actually want to replace any other character or letter APART FROM 0 - 9 and "+", "-", "(", ")", " ".

Hope that makes sense.
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,040
Members
449,063
Latest member
ak94

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