Write to CSV file - new line

johnkrenkel

New Member
Joined
Apr 2, 2019
Messages
11
I am trying to filter through a bunch of data and write information to a csv. it is functioning correctly with one exception, i keep writing over the first line so i never get all the data. I am certain it is a simple fix that I am overlooking to get to a new line in the csv file.

here is the relevant pc of code

Code:
For Each cel In rng.Cells



        item_in_review = cel.Value ' Name
        EPIC_YES = cel.Offset(0, 1).Value 'Epic YES




            If EPIC_YES = "Yes" Then '


                For Each cel2 In rng2.Cells
    
                    'Match This
                    acct_string = cel2.Offset(0, 19).Value 'AcctName
                    date_string2 = cel2.Offset(0, 17).Value 'SettDateOfTran
                    
                    'Output This
                    ret_code = cel2.Offset(0, 14).Value 'Return Code
                    id_num = cel2.Offset(0, 2).Value ' IdNum
                    tran_code = cel2.Offset(0, 6).Value 'Tran Code
        
                       If item_in_review = acct_string And Settlement = date_string2 Then
                        
                        myfile = "C:\Test\" & currdate & "\Test-" & item_in_review & currdate & ".csv"
                        FileNumber = FreeFile
                        
                        Open myfile For Output As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FileNumber]#FileNumber[/URL] 
                        Write [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FileNumber]#FileNumber[/URL] , id_num, ret_code, tran_code

                        Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FileNumber]#FileNumber[/URL] 
             
            
                        End If
    
                Next cel2
 
 
            End If




    Next cel
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
You are opening the file writing a line then clos9ng it each iteration of the loop

Try moving the filenumber and open to after the epic_yes “yes” test and moving the close to after next cel2

It might not be the best logic but a start
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,446
Members
449,083
Latest member
Ava19

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