+ Reply to Thread
Results 1 to 6 of 6

Append range to csv file

Hybrid 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.

  2. #2
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: Append range to csv file

    Change
    tmp = tmp & Chr(10) & r.Value
    to
    tmp = tmp & Chr(13) & r.Value
    David
    (*) Reputation points appreciated.

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

    Re: Append range to csv file

    Thanks david but its not working. Here is what i am trying to accomplish:

    A B
    text text
    text text

    I want the the csv file to read as;
    text,text
    tex, text

    but with the set up now it is texttexttexttexttext

    thanks again
    Last edited by jharaldson; 05-30-2013 at 08:15 PM.

  4. #4
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: Append range to csv file

    The statement to add the value to the function is missing.
        End If
        Range2CSV = tmp
        End Function
    This is how I code it.
    Function MyOption(list As Range) As String
    
    Dim A As Long
    Dim B As Long
    Dim NumRows As Long
    Dim NumCols As Long
    Dim Tmp As String
    
    NumRows = list.Rows.Count
    NumCols = list.Columns.Count
    
    For A = 1 To NumRows
        For B = 1 To NumCols
            Tmp = Tmp & Cells(A, B) & ","
        Next
        Tmp = Left(Tmp, Len(Tmp) - 1)
        Tmp = Tmp & Chr(13)
    Next
    
    MyOption = Tmp
    End Function

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

    Re: Append range to csv file

    test.csv here is a csv sample that i get when i run your code and my original code. This text here sample,text,,,,,,,,,,, is what the original file is and when i append i get whats right below it. I want it to look like the original.

    thanks again

  6. #6
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: Append range to csv file

    Here's the sample file I created. It generates the row/column as the excel sheet. Look at it with Wordpad.
    Attached Files Attached Files

+ Reply to Thread

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