vba challenge

pnuckle

New Member
Joined
Feb 12, 2011
Messages
15
i have a long list of names and i have to validate each. the criteria is that there must no name containing characters such as (, < > . / : ; " = + _ ` ~ ! * - + @ # $ % ^ & *) all these characters are invalid in any name.

the problem is that i don't know how can i use vba to remove any of those characters whenever they appear in any name.

i count on you members!

all the best.

thanx in advance.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Check out the Replace function which can be used to strip characters out of a string.

Incidentally, is that a hyphen in your list?
 
Upvote 0
Try this for data in column "A".
Code:
[COLOR="Navy"]Sub[/COLOR] MG01Mar23
[COLOR="Navy"]Dim[/COLOR] oCh [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
    oCh = Array(")", "(", ",", "<", ">", ".", "<", "/", "<", ":", ";", ",""", "=", "+", "_", "`", "~", "!", "*", "-", "+", "@", "#", "$", "%", "^", "&", "*")
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]For[/COLOR] n = 0 To UBound(oCh)
        [COLOR="Navy"]If[/COLOR] InStr(Dn, oCh(n)) [COLOR="Navy"]Then[/COLOR]
            Dn = Replace(Dn, oCh(n), "")
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]Next[/COLOR] D
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Another solutions reported range A1:
Code:
Sub test()
n = Len(Range("A1"))
For i = 1 To n
On Error Resume Next
If Asc(Mid(Range("A1"), i, 1)) > 32 And Asc(Mid(Range("A1"), i, 1)) < 39 Or _
Asc(Mid(Range("A1"), i, 1)) > 41 And Asc(Mid(Range("A1"), i, 1)) < 48 Or _
Asc(Mid(Range("A1"), i, 1)) > 57 And Asc(Mid(Range("A1"), i, 1)) < 63 Or _
Asc(Mid(Range("A1"), i, 1)) > 93 And Asc(Mid(Range("A1"), i, 1)) < 97 Or _
Asc(Mid(Range("A1"), i, 1)) = 64 Or Asc(Mid(Range("A1"), i, 1)) = 126 Then
Range("A1") = Replace(Range("A1"), Mid(Range("A1"), i, 1), "")
End If
Next
End Sub
 
Upvote 0
Just Notices !!!.
Change array for below, and add as required !!
Code:
    oCh = Array(")", "(", ",", "<", ">", ".", "<", "/", "<", ":", ";", ",", Chr(34), "=", "+", "_", "`", "~", "!", "*", "-", "+", "@", "#", "$", "%", "^", "&", "*")
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,795
Members
452,943
Latest member
Newbie4296

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