Copy data from Excel to CSV

Good Evening Experts,

I need an Excel code that will pull the data from a workbook called "Labor Totals" and place it into a CSV or comma separated text file.

I'd like it to just loop through until it doesn't find any more rows as the number of rows changes.

If possible I'd also like for it to prompt the user for a file location / filename to create.

Best Regards,
Brian
RedstoneITAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JillofallTradesMistressofNoneCommented:
Are you wanting to do something different than the user just Saving As a .csv file?  Are you only wanting certain fields exported?  
0
dlmilleCommented:
I assume you're building something off this base, or providing something that makes life easier than manually saving tabs as CSV's.  So, here goes.

The app in the attached workbook prompts the user for a SAVE AS location and filename, with defaults to .CSV.  Once selected, the Labor Total tab is COPIED to a new workbook, then saved at that path/filename, then closed.  A message box prompt confirms the save.

Here's the code (in a public module):
Sub exportToCSV()
Dim wkb As Workbook
Dim wks As Worksheet
Dim fileSaveName As Variant

    On Error GoTo errHandler
    
    Set wkb = ActiveWorkbook
    Set wks = wkb.Sheets("Labor Totals")
    
    fileSaveName = Application.GetSaveAsFilename(fileFilter:="CSV (Comma Delimited) (*.csv), *.csv", Title:="Please select a folder and specify filename to save CSV file")
    If fileSaveName <> False Then
        Application.DisplayAlerts = False
        wks.Copy 'copies to a new workbook
        ActiveWorkbook.SaveAs Filename:=fileSaveName, FileFormat:=xlCSV
        ActiveWorkbook.Close
        MsgBox "The Labor Totals tab was successfully saved as " & fileSaveName
    End If
    
    Exit Sub
    
errHandler:
    MsgBox "For some reason, you were missing the Labor Totals tab, or the file you selected had a problem  - at any rate, nothing was successfully saved" & Chr(10) & Chr(10) & "Err: " & Err.Number & " Err_Description: " & Err.Description, vbCritical, "Aborting..."
    
End Sub

Open in new window


See attached workbook.  There's a button on the demo sheet, Labor Totals for you to test.

Enjoy!

Dave
filePromptSaveCSV.xls
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.