Results 1 to 1 of 1

Export SPECIFIC worksheet from workbook.

Threaded View

  1. #1
    Registered User
    Join Date
    02-11-2018
    Location
    Pgh. Pa
    MS-Off Ver
    2013
    Posts
    2

    Export SPECIFIC worksheet from workbook.

    Give multiple worksheets in workbook, export specific worksheet (CSV) while
    converting to CSV file, removing column header and adding an empty line at the beginning.
    Using this code right now, which exports the active worksheet, does the conversion,
    etc, but in this case, I have to be in the CSV worksheet for it to function properly.
    I'd like to export CSV automatically without needing to be in.
    I tried setting a few variables and exporting but obviously Im still too much
    of a novice.
    UPDATE: After multiple hours of research and testing, most of which gave me various fun errors, I found the solution and added a line just below the Sub identifier and above the Private Sub
    Sub auto_export_CSV()
    'set CSV as active worksheet for export
    Sheets("CSV").Activate
    'Private Sub btn_Export_to_CSV_Click()
        Dim csvFilePath As String
        Dim fileNo As Integer
        Dim fileName As String
        Dim oneLine As String
        Dim lastRow, lastCol As Long
        Dim idxRow, idxCol As Long
        ' --- get this file name (without extension)
        fileName = Left(ActiveWorkbook.Name, InStrRev(ActiveWorkbook.Name, ".", -1, vbTextCompare) - 1)
        ' --- create file name of CSV file (with full path)
        csvFilePath = ActiveWorkbook.Path & "\" & fileName & ".csv"
        ' --- get last row and last column
        lastRow = Cells(Rows.Count, 1).End(xlUp).Row
        lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
        ' --- open CSC file
        fileNo = FreeFile
        Open csvFilePath For Output As #fileNo
        Print #fileNo, ""   ' -- write one blank line
        ' --- row loop
        For idxRow = 2 To lastRow
            oneLine = ""
            ' --- column loop: concatenate oneLine
            For idxCol = 1 To lastCol
                If (idxCol = 1) Then
                    oneLine = Cells(idxRow, idxCol).Value
                Else
                    oneLine = oneLine & "," & Cells(idxRow, idxCol).Value
                End If
            Next
            ' --- write oneLine > CSV file
            Print #fileNo, oneLine  ' -- Print: no quotation (output oneLine as it is)
        Next
        ' --- close file
        Close #fileNo
        MsgBox "CSV file completed !!" & Chr(13) & csvFilePath
    End Sub
    Moderator's note: Please take the time to review our rules. There aren't many, and they are all important. Rule #3 requires code tags. I have added them for you this time because you are a new member. --6StringJazzer
    Last edited by Efeid; 02-13-2018 at 12:22 PM. Reason: resolved

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 4
    Last Post: 02-14-2017, 11:21 AM
  2. Export data to a specific worksheet
    By neilsy1 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-19-2017, 05:36 AM
  3. Export a specific worksheet to another excel file.
    By paradoxofthecat in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-01-2015, 10:16 AM
  4. Export Specific Columns from a Worksheet as csv
    By Saarang84 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-12-2015, 01:06 PM
  5. [SOLVED] Export/Save a worksheet in a workbook without changing the name of the workbook?
    By 111StepsAhead in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-24-2013, 06:04 PM
  6. Export specific print page to new workbook?
    By Rerock in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-06-2012, 09:52 PM
  7. Save\export specific worksheet error
    By SarahPintal in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-23-2010, 05:12 PM

Tags for this Thread

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