Loop & append to csv file

jalea148

Board Regular
Joined
Mar 23, 2012
Messages
58
I have a table starting in some row and column.
Each row starts with a date with values in the following 5 cells and a csv file name [full address] in the last column.
I'd like to loop through each row of the table appending the date and the 5 cells to the correspond files.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
This version uses generic FileSystemObjects to collect your cells and write them to the defined files. Minimal Error checking.
It prompts for the starting cell-intended to be the first date; then it will go through all entries for that column, going down the sheet.
If the output file is not found, it is created and written to.

Code:
Option Explicit

Dim fso, f, ts 'Scripting Objects
Dim start, cell As Range
Dim r, lr, wrkcol As Long
Const dlmtr = "," ' Set Delimeter
Dim tmp As String
Sub foo()
'Init Objects
Set fso = CreateObject("Scripting.FileSystemObject")

'Get the start range
    Do
        Set start = Application.InputBox("Select some row and column [Date] to start:", Type:=8)
    Loop While start Is Nothing

    'assign variable values
    wrkcol = start.Columns(1).Column
    lr = Cells(Rows.Count, wrkcol).End(xlUp).Row

    For r = start.Rows(1).Row To lr
        tmp = ""
        Set cell = Cells(r, wrkcol)
        'build a string to write
        tmp = tmp & Cells(r, wrkcol).Offset(0, 0).Value & dlmtr    'date
        tmp = tmp & Cells(r, wrkcol).Offset(0, 1).Value & dlmtr
        tmp = tmp & Cells(r, wrkcol).Offset(0, 2).Value & dlmtr
        tmp = tmp & Cells(r, wrkcol).Offset(0, 3).Value & dlmtr
        tmp = tmp & Cells(r, wrkcol).Offset(0, 4).Value & dlmtr
        tmp = tmp & Cells(r, wrkcol).Offset(0, 5).Value

        'Determine file If Exist/Else Create
        On Error Resume Next
        Set f = fso.GetFile(Cells(r, wrkcol).Offset(0, 6).Text)    'filepath
        If Err = 53 Then
            Set f = fso.CreateTextFile(Cells(r, wrkcol).Offset(0, 6).Text, False, False)     'filepath
            Set f = fso.GetFile(Cells(r, wrkcol).Offset(0, 6).Text)    'filepath
            Err = 0
        End If
        
        'open file, write and close
        Set ts = f.OpenAsTextStream(IOMode:=ForAppending)
        ts.WriteLine tmp
        ts.Close
    Next r

End Sub
 
Upvote 0
This version uses generic FileSystemObjects to collect your cells and write them to the defined files. Minimal Error checking.
It prompts for the starting cell-intended to be the first date; then it will go through all entries for that column, going down the sheet.
If the output file is not found, it is created and written to.

Code:
Option Explicit

Dim fso, f, ts 'Scripting Objects
Dim start, cell As Range
Dim r, lr, wrkcol As Long
Const dlmtr = "," ' Set Delimeter
Dim tmp As String
Sub foo()
'Init Objects
Set fso = CreateObject("Scripting.FileSystemObject")

'Get the start range
    Do
        Set start = Application.InputBox("Select some row and column [Date] to start:", Type:=8)
    Loop While start Is Nothing

    'assign variable values
    wrkcol = start.Columns(1).Column
    lr = Cells(Rows.Count, wrkcol).End(xlUp).Row

    For r = start.Rows(1).Row To lr
        tmp = ""
        Set cell = Cells(r, wrkcol)
        'build a string to write
        tmp = tmp & Cells(r, wrkcol).Offset(0, 0).Value & dlmtr    'date
        tmp = tmp & Cells(r, wrkcol).Offset(0, 1).Value & dlmtr
        tmp = tmp & Cells(r, wrkcol).Offset(0, 2).Value & dlmtr
        tmp = tmp & Cells(r, wrkcol).Offset(0, 3).Value & dlmtr
        tmp = tmp & Cells(r, wrkcol).Offset(0, 4).Value & dlmtr
        tmp = tmp & Cells(r, wrkcol).Offset(0, 5).Value

        'Determine file If Exist/Else Create
        On Error Resume Next
        Set f = fso.GetFile(Cells(r, wrkcol).Offset(0, 6).Text)    'filepath
        If Err = 53 Then
            Set f = fso.CreateTextFile(Cells(r, wrkcol).Offset(0, 6).Text, False, False)     'filepath
            Set f = fso.GetFile(Cells(r, wrkcol).Offset(0, 6).Text)    'filepath
            Err = 0
        End If
        
        'open file, write and close
        Set ts = f.OpenAsTextStream(IOMode:=ForAppending)
        ts.WriteLine tmp
        ts.Close
    Next r

End Sub
I'm getting a compile error in this line:
Set ts = f.OpenAsTextStream(IOMode:=ForAppending)
It says ForAppending is not defined. I replaced ForAppending with 8 and it works like a charm!

Thanks for your help.
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

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