Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Junior Member
 
Posts: 1
Default Macro for Append to csv

Hi guys,
I'm new here, if I'm posting in the wrong place, feel free to yell at me.
I have a dynamic range, which is defined by the number of non empty rows in a worksheet. (typically 10-20 rows). The number of columns are awlays the same (6). So, If I have 10 rows, my range would be A1:F10.
Now, I need a macro to append this range to an existing csv file, and have no idea how to do it, my abilities with VBA are limited, to say the leat.
Any ideas?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 538
Default Macro for Append to csv

rooperi wrote:

I'm new here, if I'm posting in the wrong place, feel free to yell at
me.
I have a dynamic range, which is defined by the number of non empty rows
in a worksheet. (typically 10-20 rows). The number of columns are awlays
the same (6). So, If I have 10 rows, my range would be A1:F10.
Now, I need a macro to append this range to an existing csv file, and
have no idea how to do it, my abilities with VBA are limited, to say the
leat.


Try this:

Sub AppendToExistingCSV()
Dim ro As Range, cl As Range, fn As Long, outP As String
fn = FreeFile
Open "C:\full\path\to\your.csv" For Append As fn
For Each ro In Range("A1:" & Cells.SpecialCells(xlCellTypeLastCell) _
.Address).Rows
outP = ""
For Each cl In ro.Columns
If VarType(cl.Cells.Value) = vbString Then
outP = outP & """" & CStr(cl.Cells.Value) & ""","
Else
outP = outP & CStr(cl.Cells.Value) & ","
End If
Next
If outP < String(ro.Columns.Count, ",") Then
Print #fn, Left(outP, Len(outP) - 1)
'To continue after blank line, remove next 2 lines:
Else
Exit For
End If
Next
Close fn
End Sub

(As with everything I post, almost certainly not the best way to do it,
but it works.)

A few notes:
- Change "C:\full\path\to\your.csv" to point at the actual CSV.
- The path to the CSV (but not necessarily the CSV itself) must already
exist. (This will create the file if needed, but not the directory/ies.)
- The CSV can't already be open in Excel (or any other program that locks
files for writing) or this will fail. (You'll probably get error 70,
"Permission denied".)
- If you want this to continue on after encountering a blank line, delete
the two lines I marked.

--
Are you saying I can offend you by complementing other people?
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
macro to append ones files xls into one ? carlos diaz Excel Programming 0 April 11th 08 07:28 PM
Copy and Append Macro Excel Help! Excel Discussion (Misc queries) 2 April 7th 08 05:39 PM
Macro to append En Spaces Dan[_48_] Excel Programming 1 October 8th 05 01:15 AM
macro to append one cell to another billbeecham[_2_] Excel Programming 0 November 11th 04 07:45 PM
macro to append one cell to another billbeecham Excel Programming 4 November 10th 04 01:23 PM


All times are GMT +1. The time now is 12:59 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"