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 Apr 6th, 2002, 07:44 AM   #1
Jak
Board Regular
 
Jak's Avatar
 
Join Date: Apr 2002
Location: Newcastle upon Tyne
Posts: 762
Default


I use Excel to edit and arrange data into fields that I import to an Access Database. 70% of the time the data is non problematic. On those occasions when it is it takes a bit of time to delete unwanted formatting etc. One such problem is when I receive CSV files. Sometimes there are spaces after the text portions. This means that when the file is imported as comma delimited and a comma separates each field, there are still unwanted spaces after each text portion. I began by highlighting all the data and replacing the spaces with nothing. This was fine until I noticed that names that were originally separated by a space were now joined and had to have the spaces put back in. Small file sizes are quick to edit but larger files take an age.

As an example:

John James,123,Andrew Spencer,Y,123456,A,M

The above is an example of what each field might contain. There are no spaces after each text portion. Alternatively, here is a problematic example:

John James ,123 ,Andrew Spencer ,Y ,123456 ,A ,M

This is caused because the original data has spaces after each text portion. By using replace all spaces with nothing I end up with this:

JohnJames,123,AndrewSpencer,Y,123456,A,M

There is now no space between John and James or Andrew and Spenser. What I would like is a method of replacing the unwanted spaces without joining separate texts together. Any help in this capacity would be welcomed.


[ This Message was edited by: Jak on 2002-05-15 06:29 ]
Jak is offline   Reply With Quote
Old Apr 6th, 2002, 07:54 AM   #2
Chris Davison
MrExcel MVP
 
Join Date: Feb 2002
Location: Millbank, London, UK
Posts: 1,790
Default

hmmm

could you replace " ," with just ","

ie " SPACE COMMA" with just "COMMA" ?

this would only capture cases of a space where there is an associated comma seperator with your required comma
__________________
:: Pharma Z - Family drugstore ::
Chris Davison is offline   Reply With Quote
Old Apr 7th, 2002, 03:47 AM   #3
Jak
Board Regular
 
Jak's Avatar
 
Join Date: Apr 2002
Location: Newcastle upon Tyne
Posts: 762
Default

Cheers Chris

I will give your solution a go. Thanks
Jak is offline   Reply With Quote
Old Apr 7th, 2002, 03:54 AM   #4
Dave Hawley
Banned
 
Join Date: Feb 2002
Posts: 1,582
Default

Hi Jack


The Text to Columns feature found under Data can help with these sort of problems.

The other helpful function is the TRIM function, eg TRIM(A1)



Dave Hawley is offline   Reply With Quote
Old Apr 7th, 2002, 05:04 AM   #5
Jak
Board Regular
 
Jak's Avatar
 
Join Date: Apr 2002
Location: Newcastle upon Tyne
Posts: 762
Default

Hi Dave

I have not used the Trim function. I will have a look in Excel help menu for sample data. I have been working on a macro based on previous idea provided by Chris. The macro does remove all the spaces but causes a run time error as follows:

Run-time error '91':

object variable or with block variable not set

I am not sure how to go about debugging this error. Here is the macro code I have put together. Any help with the debug would be welcomed. I the meantime I will have a look at the Trim function. Cheers

Sub Macro2()

Application.ScreenUpdating = False

Do While ActiveCell > 1
ActiveCell.Replace What:=" ,", Replacement:=",", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
Cells.Find(What:=" ,", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _
.Activate
Loop

Application.ScreenUpdating = True

End Sub
Jak is offline   Reply With Quote
Old Apr 7th, 2002, 05:10 AM   #6
Dave Hawley
Banned
 
Join Date: Feb 2002
Posts: 1,582
Default

Hi Jack

Here is a nice simply very fast macro. Just select your data Column before running it.


Code:
Sub GoAwaySpaces()
 Dim rCells As Range
 
 Set rCells = Range(Selection.Cells(1, 1), _
                    Selection.Cells(65536, 1).End(xlUp))
                    
 rCells.EntireColumn.Insert
 rCells.Offset(0, -1).FormulaR1C1 = "=TRIM(RC[1])"
 rCells.Offset(0, -1) = rCells.Offset(0, -1).Value
 
 End Sub
Dave Hawley is offline   Reply With Quote
Old Apr 7th, 2002, 05:41 AM   #7
Jak
Board Regular
 
Jak's Avatar
 
Join Date: Apr 2002
Location: Newcastle upon Tyne
Posts: 762
Default

Many thanks Dave for the Trim code. I do like the macros name. I will try it out and advise if there are any problems.
Jak 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 06:29 AM.


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