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 16th, 2002, 06:31 PM   #1
davers5
Board Regular
 
Join Date: Feb 2002
Posts: 255
Default

I have a bad CSV file with rows of data that have varying numbers of elements in them. I want to read the values into arrays depending on the first value in a line. Here is an example:

"Joe", "Miller", "$545.67", "CA"
"", "00-6500", "$342.34"
"Mary", "Jones", "$778.67", "MA"
"$342.21", "$564.00", "$980.00", "432", "1995"

I'd like to be able to go through this file and only read the lines of data that start with nothing ("") or a number. I can do the testing to see if the values are what I'm looking for, my question is: how do I skip to the next line after I figure out that I don't want the data in the current line?

I'd the above data to be printed as (in excel with the spaces denoting cell seperation):
00-6500 $342.34
$342.21 $564.00 $980.00 432 1995

Thanks for your help!

Dave
davers5 is offline   Reply With Quote
Old Apr 16th, 2002, 07:06 PM   #2
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

Hi
Run this routine to open the damaged file and save the data you want to a new file.

Two dialogs will appear.
The first will be for the file you are wanting to open. The second for the new file.

Let me know if the filtering depth is adequate or not.

Thanks,
Tom

Sub CleanFile()
Dim FileToOpen
Dim FileToSave
Dim FileNum(2) As Integer
Dim ThisRow

FileToOpen = Application.GetOpenFilename
If FileToOpen = False Then Exit Sub
FileToSave = Application.GetSaveAsFilename
If FileToSave = False Then Exit Sub

FileNum(1) = FreeFile
Open FileToOpen For Input As #FileNum(1)
FileNum(2) = FreeFile
Open FileToSave For Output As #FileNum(2)
Do Until EOF(FileNum(1))
Line Input #FileNum(1), ThisRow
Debug.Print ThisRow

If IsNumeric(Mid(ThisRow, 2, 1)) Or _
IsNumeric(Mid(ThisRow, 3, 1)) Or _
Mid(ThisRow, 2, 1) = """" Then _
Print #FileNum(2), ThisRow
Loop
Close #FileNum(1)
Close #FileNum(2)
End Sub

Tom Schreiner is offline   Reply With Quote
Old Apr 17th, 2002, 10:19 AM   #3
davers5
Board Regular
 
Join Date: Feb 2002
Posts: 255
Default

Thanks TsTom! I figured it was probably best to use a line input statement and then test the text string. I guess that's what I'll do. Thanks for sharing your code!

Dave
davers5 is offline   Reply With Quote
Old Apr 17th, 2002, 01:19 PM   #4
davers5
Board Regular
 
Join Date: Feb 2002
Posts: 255
Default

Ok, so I'm having another problem. For some reason, when I write a full line to a file it adds a bunch of quotes. So when I read from the file, I can't seperate the values correctly. For example:

I read the whole line into a variable:
line input #1, strFullLine
Here's a value from the initial file I get data from:
"","60-1111-0000-000","$28.84"
I test the data and if I want it, I print the line to an output file:
Print #2, strFullLine
In the new file it becomes:
","60-1111-0000-000","$28.84"
I tried using
Write #2, strFullLine
And with that syntax it becomes:
""",""60-1111-0000-000"",""$28.84"""

Is there anyway to solve this? I've tried using Instr() to find single quotations(") but since they're special characters you can't just search for one.

Thanks for the advice,

Dave
davers5 is offline   Reply With Quote
Old Apr 17th, 2002, 01:26 PM   #5
davers5
Board Regular
 
Join Date: Feb 2002
Posts: 255
Default

Ok, I solved the problem. Thanks for listening! My problem was the difference between the PRINT keyword and the WRITE keyword. I tried to get rid of some qoutes by using a mid statement because I was using WRITE instead of PRINT. So when I switched to PRINT (upon further looking at TsTom's code), I kept the mid statement but I no longer needed it.

My question now becomes what's the difference between Print and Write?

Dave
davers5 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 01:52 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