Consulting

Results 1 to 7 of 7

Thread: Saving as .csv

  1. #1
    VBAX Regular
    Joined
    Dec 2004
    Posts
    93
    Location

    Question Saving as .csv

    Hi guys,
    below is the part of the code when I am taking the data from a sheet and save it as *.csv file.

    'Export data
        vFF = FreeFile 
        Open vSaveFile For Output As #vFF 
        Print #vFF, Sheets(2).Range("A1").Text 'line 1
        Print #vFF, CStr(Month(Sheets(2).Range("A2"))) 'line 2
        Print #vFF, CStr(Month(Sheets(2).Range("A2"))) 'line 3
        Print #vFF, "Actual" 'line 4
        For i = 0 To ExpCount - 2 
            Print #vFF, ExpAcct(i) 'lines 5 through second last account
        Next i 
        Print #vFF, ExpAcct(ExpCount - 1); 'last account ends in ; to prevent extra line feed
        Close #vFF
    Now, I need to change it a little.
    Sheets(2).Range("A1").Text in line 1 will be in Column A of my *.csv file. I need to add some value in line 1 that will be in Column B of my *.csv file. How to do it?

    Thanks in advance.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Do you mean?

    Sheets(2).Range("B1").Text

  3. #3
    VBAX Regular
    Joined
    Dec 2004
    Posts
    93
    Location
    It can be
    Sheets(2).Range("B1").Text
    but in my case it will be a string="Actual".

  4. #4
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    How is this code saving as a CSV?

  5. #5
    VBAX Regular
    Joined
    Dec 2004
    Posts
    93
    Location
    OK, here is the complete code:


    Sub KaizerExportCSV() Dim vFF As Long, vSaveFile As String Dim AllAcctData, ExpAcct() As String, ExpCount As Long, i As Long Dim RG As Range 'Account data range, starting in row 2 to account for headers Set RG = Intersect(Sheets(1).Range("A2:B65536"), Sheets(1).UsedRange) If RG Is Nothing Then Exit Sub 'no data on sheet 1 AllAcctData = RG.Value 'Create array of data to export, using only non-zero and non-blank account values ExpCount = 0 For i = LBound(AllAcctData, 1) To UBound(AllAcctData, 1) If AllAcctData(i, 2) <> 0 And AllAcctData(i, 2) <> "" Then ReDim Preserve ExpAcct(ExpCount) ExpAcct(ExpCount) = AllAcctData(i, 1) & "," & AllAcctData(i, 2) ExpCount = ExpCount + 1 End If Next i If ExpCount = 0 Then Exit Sub 'No non-zero values 'Get save file name vSaveFile = Application.GetSaveAsFilename(InitialFilename:=Left(ActiveWorkbook.Name, _ Len(ActiveWorkbook.Name) - 4) & ".csv", filefilter:="CSV Files,*.csv,All Files,*.*") If LCase(vSaveFile) = "false" Then Exit Sub 'user hit cancel 'Export data vFF = FreeFile Open vSaveFile For Output As #vFF Print #vFF, Sheets(2).Range("A1").Text 'line 1 Print #vFF, CStr(Month(Sheets(2).Range("A2"))) 'line 2 Print #vFF, CStr(Month(Sheets(2).Range("A2"))) 'line 3 Print #vFF, "Actual" 'line 4 For i = 0 To ExpCount - 2 Print #vFF, ExpAcct(i) 'lines 5 through second last account Next i Print #vFF, ExpAcct(ExpCount - 1); 'last account ends in ; to prevent extra line feed Close #vFF End Sub

  6. #6
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi Kaizer,

    I've edited your post to use our VBA tags. [uvba]a[/uvba]. (No need to do all the colouring yourself!)

    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  7. #7
    VBAX Regular
    Joined
    Dec 2004
    Posts
    93
    Location
    I have found the way to treat this operation. No worries.

Posting Permissions

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