Smart macro to create proper CSV files?

AJ

Active Member
Joined
Mar 4, 2002
Messages
478
This is a follow up from an old post...

http://www.mrexcel.com/board/viewtopic.php?topic=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
 
Thank you all very much for the help.
Daleyman was absolutely spot on so thanks very much.
TsTom, like Daleyman I think I understand your points but as yet lack the know how to take them further! However, as Daleyman's code works beautifully, I'm a very happy chappy!
nisht, thanks for the input, but that way doesn't put in the quotes around every field.
It is of course fine, if the quotes are not essential. I just have a rather backward package where they are essential!

Cheers!
AJ
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top