MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old May 1st, 2002, 04:52 AM   #1
AJ
Board Regular
 
Join Date: Mar 2002
Location: =ActiveCell.Address
Posts: 478
Default

Hi folks,

Got a weird-ish one here.
What I need to do is to find a quick way of removing all the following characters from a cell:
/

*
:
<
>
|
~
Basically all those characters that cannot be used in a filename as well as the tilde ~.
Alternatively, just remove any character that is not a alpha character or a number.
I also would like to dump extra white space (TRIM) and non-printable (CLEAN)

Here's the tricky bit though - I want to do it in one step, not with an endless procession of SUBSTITUTE, TRIM AND CLEAN.

In fact, to make it perfect, I don't really want to do it to a cell value at all, but rather a variable in VBA, although knowing the way to do it to a single cell would be just as good.

Any ideas?

Many thanks in advance
AJ
AJ is offline   Reply With Quote
Old May 1st, 2002, 05:20 AM   #2
s-o-s
Board Regular
 
s-o-s's Avatar
 
Join Date: Apr 2002
Location: Kissimmee, Florida
Posts: 384
Default

Here is some VB code that will clean up the values in the current cell. If you find you need to add more characters to the excluded list, just add them in to the instr line.

Sub Clean_It_up()
Dim CurVal As Variant
Dim Count As Integer
Dim CurChr As Variant
Dim NewVal As Variant

CurVal = ActiveCell.Value

For Count = 1 To Len(CurVal)
CurChr = Mid(CurVal, Count, 1)
If InStr(1, "/*:<>|~", CurChr) = 0 Then
NewVal = NewVal & CurChr
End If
Next

ActiveCell.Value = NewVal
End Sub

__________________
Hope This Helps.
Sean.
Digest of Homes

WinXP, XL XP
s-o-s is offline   Reply With Quote
Old May 1st, 2002, 05:53 AM   #3
AJ
Board Regular
 
Join Date: Mar 2002
Location: =ActiveCell.Address
Posts: 478
Default

Brilliant - works a charm!
Thanks
AJ
AJ is offline   Reply With Quote
Old Mar 17th, 2004, 04:33 PM   #4
cazan
New Member
 
Join Date: Feb 2004
Location: Dayton, Ohio
Posts: 38
Default Re: How to clean a cell value or a variable

I have found a need for this code too. But what modifications do I need to make to it in order to clean up a couple thousand items in a column with some cells being empty?

Thanks in Advance
Chris A. Z.
cazan is offline   Reply With Quote
Old Mar 17th, 2004, 05:10 PM   #5
Joe4
MrExcel MVP
Moderator
 
Joe4's Avatar
 
Join Date: Aug 2002
Posts: 20,620
Default Re: How to clean a cell value or a variable

cazan,

Here is a modification of the code posted that will work on a whole column. Change the range reference as needed:

Code:
Sub Clean_It_up()

    Application.ScreenUpdating = False

    Dim Count As Integer
    Dim CurChr As Variant
    Dim NewVal As Variant
    Dim myRange As Range
    Dim cell As Range
    
'   Enter range to clean (column A in this example)
    Set myRange = Range("A1:A" & Range("A65536").End(xlUp).Row)
    
    For Each cell In myRange
        NewVal = ""
        For Count = 1 To Len(cell)
            CurChr = Mid(cell, Count, 1)
            If InStr(1, "/*:<>|~", CurChr) = 0 Then
                NewVal = NewVal & CurChr
            End If
        Next
        cell.Value = NewVal
    Next

    Application.ScreenUpdating = True

End Sub
__________________
TIPS FOR FINDING EXCEL SOLUTIONS
1. Use the built-in Help that comes with Excel/Access
2. Use the Search functionality on this board
3. A lot of VBA code can be acquired by using the Macro Recorder.

"Give a man a fish, feed him for a day. Teach a man to fish, feed him for life!"
Joe4 is offline   Reply With Quote
Old Mar 18th, 2004, 08:13 PM   #6
cazan
New Member
 
Join Date: Feb 2004
Location: Dayton, Ohio
Posts: 38
Default Re: How to clean a cell value or a variable

Thank you! I works great. I would image that if I were to clean a range or whole row, all I would have to do is place the row range where the column range, ("A1:A" & Range("A65536"), is located.

Is the row range syntax different than the column?

Thanks you,
Chris A. Z.
cazan is offline   Reply With Quote
Old Mar 18th, 2004, 08:28 PM   #7
DRJ
MrExcel MVP
 
DRJ's Avatar
 
Join Date: Feb 2002
Location: California
Posts: 3,857
Default

Yeah, just change MyRange to whatever range you want and the macro will check every cell in that range.
__________________
Excel VBA Training and Certification (Lesson 1 is free)
<hr>

<hr>-Jacob
DRJ is offline   Reply With Quote
Old Mar 18th, 2004, 09:39 PM   #8
jholzman1
New Member
 
Join Date: Mar 2004
Posts: 5
Default Re: How to clean a cell value or a variable

There's also a super-easy utility out there -- check out http://www.asap-utilities.com. It's great. And one of the tools does exactly what you're asking for. (Plus, there's tons more.)
jholzman1 is offline   Reply With Quote
Old Mar 18th, 2004, 10:17 PM   #9
Ekim
Board Regular
 
Join Date: Jun 2002
Location: Perth, Australia
Posts: 1,416
Default Re: How to clean a cell value or a variable

In the previous macro, you may wish to change this line:

cell.Value = NewVal

to:

cell.Value = Application.Clean(Application.Trim(NewVal))

I note that the while the macro removes the designated special characters, the deleted characters are represented as spaces in the text string.

Notes:
VBA 's Trim function only trims leading and trailing spaces, not extra internal spaces. If you want to trim those as well, you need to use the worksheet Trim function (hence “Application.Trim”)

The CLEAN function removes all nonprintable characters from a string.

Regards,

Mike
Ekim is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 12:37 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes