Results 1 to 6 of 6

Append range to csv file

Threaded View

  1. #1
    Registered User
    Join Date
    12-09-2011
    Location
    Sioux Falls, South Dakota
    MS-Off Ver
    Excel 2007
    Posts
    46

    Append range to csv file

    Hello all,
    What I am trying to do is append a range on my spreadsheet to a csv file. I found this code, it works but it appends everything in one row. I want it to be in 2 columns in the csv file.
    Any help would be awesome.

    thanks


    
    Sub Append2CSV()
    Dim tmpCSV As String 'string to hold the CSV info
    Dim f As Integer
    
    Const CSVFile As String = "C:\test.csv" 'replace with your filename
    f = FreeFile
    
    Open CSVFile For Append As #f
    tmpCSV = Range2CSV(Range("A1:B10"))
    Print #f, tmpCSV
    Close #f
    
    End Sub
    
    Function Range2CSV(list) As String
    Dim tmp As String
    Dim cr As Long
    Dim r As Range
    
    If TypeName(list) = "Range" Then
    cr = 1
    
    For Each r In list.Cells
    If r.Row = cr Then
    If tmp = vbNullString Then
    tmp = r.Value 
    Else
    tmp = tmp & "," & r.Value
    End If
    Else
    cr = cr + 1
    If tmp = vbNullString Then
    tmp = r.Value
    Else
    tmp = tmp & Chr(10) & r.Value
    End If
    End If
    Next
    End If
    Last edited by jharaldson; 05-31-2013 at 03:03 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1