![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Board Regular
Join Date: Mar 2002
Location: =ActiveCell.Address
Posts: 478
|
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 |
|
|
|
|
|
#2 |
|
Board Regular
Join Date: Mar 2002
Location: =ActiveCell.Address
Posts: 478
|
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 |
|
|
|
|
|
#3 |
|
Board Regular
Join Date: Mar 2002
Location: London, UK
Posts: 167
|
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> |
|
|
|
|
|
#4 |
|
MrExcel MVP
Join Date: Mar 2002
Location: Michigan USA
Posts: 11,452
|
Hi AJ:
Forgive me if I am misreading you -- would a TEXT FILE not always be a single column file? |
|
|
|
|
|
#5 |
|
Board Regular
Join Date: Mar 2002
Location: London, UK
Posts: 167
|
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> |
|
|
|
|
|
#6 |
|
Board Regular
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
|
Comma Seperated Values
|
|
|
|
|
|
#7 |
|
Board Regular
Join Date: Mar 2002
Location: London, UK
Posts: 167
|
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> |
|
|
|
|
|
#8 |
|
Board Regular
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
|
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 |
|
|
|
|
|
#9 |
|
Board Regular
Join Date: Mar 2002
Location: London, UK
Posts: 167
|
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> |
|
|
|
|
|
#10 |
|
Board Regular
Join Date: Feb 2002
Location: Ahmedabad Gujarat
Posts: 303
|
Why dont you try this way
sub cvsfilesave () thisworkbook.SaveAs "d:/nisht",xlcvs end sub nishith desai |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|