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 12th, 2002, 04:40 AM   #1
AJ
Board Regular
 
Join Date: Mar 2002
Location: =ActiveCell.Address
Posts: 478
Default

This is a follow up from an old post...

http://www.mrexcel.com/board/viewtop...c=3508&forum=2

Babytiger wrote a beautiful bit of code (below) to create proper a CSV file
(i.e. with quote marks around each field).
I wondered whether anyone can help me refine it for my purposes.

Open "C:FileName.txt" For Output As #1
For i = 1 To 10
Print #1, """" & Range("A1").Offset(i - 1, 0).Value & """" & "," & _
"""" & Range("A1").Offset(i - 1, 1).Value & """" & "," & _
"""" & Range("A1").Offset(i - 1, 2).Value & """"
Next i
Close #1

I have to create files like this all the time. Unfortunately, they are always different with a variable amount of rows and columns. While I can easily see how to adjust the code for the number of rows to create, I've been racking my brain and just cannot work out a way to make it so that it processes a variable number of columns.

Basically, the perfect final version of this macro for me would be to read the number of rows and columns in the spreadsheet and create a file accordingly without the user having to edit the macro in anyway.

Can anyone help?
It's driving me nuts as I know I could be so close to a wicked solution that would save me hours and hours of formating!

Rgds
AJ

AJ is offline   Reply With Quote
Old Apr 12th, 2002, 09:22 AM   #2
AJ
Board Regular
 
Join Date: Mar 2002
Location: =ActiveCell.Address
Posts: 478
Default

Apologies for pushing this back up to the top again, but if someone could even just give me enough to get started (i.e. the sort of thing I could search for here or in VBA help) I'd be most grateful.

Thank you all!
Have a great weekend!

AJ
AJ is offline   Reply With Quote
Old Apr 13th, 2002, 05:59 PM   #3
daleyman
Board Regular
 
Join Date: Mar 2002
Location: London, UK
Posts: 167
Default

nice question.




Sub output_file()
'determine number of columns from row 1

maxcol = 1
Do Until Cells(1, maxcol + 1).Value = ""
maxcol = maxcol + 1
Loop

'output file until value in column a is empty

Open "c:filename.csv" For Output As #1
rowx = 1
Do Until Cells(rowx + 1, 1).Value = ""
strg = ""
For colx = 1 To maxcol
If colx > 1 Then strg = strg & ","
strg = strg & Chr(34) & Cells(rowx, colx).Value & Chr(34)
Next
Print #1, strg
rowx = rowx + 1
Loop
Close #1
End Sub

__________________

<table style="background-color:#0e54be" cellspacing="1" cellpadding="2"><td style="background-color:#ceffff;font-family:arial;color:#072c63;font-size:8pt;">***DALEY** :P**</td></table>
daleyman is offline   Reply With Quote
Old Apr 13th, 2002, 09:12 PM   #4
Yogi Anand
MrExcel MVP
 
Join Date: Mar 2002
Location: Michigan USA
Posts: 11,452
Default

Hi AJ:
Forgive me if I am misreading you -- would a TEXT FILE not always be a single column file?
Yogi Anand is offline   Reply With Quote
Old Apr 13th, 2002, 09:17 PM   #5
daleyman
Board Regular
 
Join Date: Mar 2002
Location: London, UK
Posts: 167
Default

would be a single column on the file i guess, but each row is made up of columns with each value in quotes and delimited by a comma....


"A1","B1","C1","D1"
"A2","B2","C2","D2"

hence .csv (comma delimited text-file)...

heh, does anyone know what C-S-V actually stands for, i'm guessing comma-something-something....
__________________

<table style="background-color:#0e54be" cellspacing="1" cellpadding="2"><td style="background-color:#ceffff;font-family:arial;color:#072c63;font-size:8pt;">***DALEY** :P**</td></table>
daleyman is offline   Reply With Quote
Old Apr 13th, 2002, 09:23 PM   #6
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

Comma Seperated Values
Tom Schreiner is offline   Reply With Quote
Old Apr 13th, 2002, 09:26 PM   #7
daleyman
Board Regular
 
Join Date: Mar 2002
Location: London, UK
Posts: 167
Default

ah ha! hee hee
__________________

<table style="background-color:#0e54be" cellspacing="1" cellpadding="2"><td style="background-color:#ceffff;font-family:arial;color:#072c63;font-size:8pt;">***DALEY** :P**</td></table>
daleyman is offline   Reply With Quote
Old Apr 13th, 2002, 09:28 PM   #8
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

Daley,
Am not sure, but since you are assigning all values to one string, you may lose data type info?

Might want to use the write statement with an array.

Example:

Redim ToCsv(1 to maxcol)

Select Case maxcol
Case 1
Write#1,ToCsv(1)
Case 2
Write#1,ToCsv(1),ToCsv(2)
Case 3
Ect...
End Select

Tom

Tom Schreiner is offline   Reply With Quote
Old Apr 13th, 2002, 09:35 PM   #9
daleyman
Board Regular
 
Join Date: Mar 2002
Location: London, UK
Posts: 167
Default

yeah, understand your point even if not your code. case if would certainly be able to tackle the scenarios, depends what the application is.

Personally, i wouldn't put quotes around everything either, just use commas, keeps the file size down, but it was in the question.
__________________

<table style="background-color:#0e54be" cellspacing="1" cellpadding="2"><td style="background-color:#ceffff;font-family:arial;color:#072c63;font-size:8pt;">***DALEY** :P**</td></table>
daleyman is offline   Reply With Quote
Old Apr 13th, 2002, 11:29 PM   #10
nisht
Board Regular
 
Join Date: Feb 2002
Location: Ahmedabad Gujarat
Posts: 303
Default

Why dont you try this way
sub cvsfilesave ()
thisworkbook.SaveAs "d:/nisht",xlcvs
end sub

nishith desai
nisht 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 07:32 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