Try this. Change this statement to the number of rows you have
For i = 2 to 100, if you have 1000 rows, make it For i = 2 to 1000.
Main Topics
Browse All TopicsI need to make an excel macro vba to remove bad characters (special characters) in a string of text, such as (, commas, -, ;, :, !, \n, /n, etc, but I dont know the best way to handle&
I want to remove them because they are not getting loaded into the SQL server DB when I run the insert macro.
This is what I want to do:
If the string in column C and row 2 (C2) is computer parts: (disk drive), then I want to delete the special characters, which in this case is ":", ( and ")".
If the above isnt true&then continue to the next row in that same column, which is C3. I am using column C and the header is at C1.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
superoneio,
In my example, I used the pattern string:
^ +| +$|\\n|/n|[^a-z0-9 ]
In RegExp, the | character acts as a logical or operator. So, reading left to right, I try to match:
1) One or more leading spaces OR
2) One or more trailing spaces OR
3) \n (it's \\n in the pattern because \ is a special character) OR
4) /n OR
5) Any other character besides a-z, 0-9, or a space
If any of the above are found they are replaced with a zero-length string, effectively deleting them.
If that pattern is not quite what you are looking for, then please restate the rules as clearly as you can,
and I can revise it.
Patrick
alt + F* runs a macro but I assume that's what you meant. It won't make a difference since I already ran it. Note hoewever as far as I can tell you only wanted to exclude the characters:
:,()
ANd since these were already excluded when I ran it there will be no difference. If you want to exclude more characters then of course let me know. Also note that it is now dynamic and will auto exclude the unwanted characters ... try adding one of the four characters to column C and see what happens when you tab or enter.
Chris
Business Accounts
Answer for Membership
by: matthewspatrickPosted on 2009-08-18 at 12:45:00ID: 25126817
Hello superoneio,
Assuming that you want only letters, digits, and spaces, add the UDF below to your VB Project, and
then use a formula like this to "clean up" the data:
=RegExpReplace(A2,"^ +| +$|\\n|/n|[^a-z0-9 ]","",True,False)
Regards,
Patrick
Select allOpen in new window