VBA Saving single sheet as CSV (not whole workbook)

后端 未结 3 469
悲&欢浪女
悲&欢浪女 2020-12-10 11:02

I appreciate there are lots of entries like save individual excel sheets as csv and Export each sheet to a separate csv file - But I want to save a single worksheet<

相关标签:
3条回答
  • 2020-12-10 11:49

    This code works fine for me.

    Sub test()
    
    Application.DisplayAlerts = False
    
    ThisWorkbook.Sheets(strSourceSheet).Copy
    ActiveWorkbook.SaveAs Filename:=strFullname, FileFormat:=xlCSV, CreateBackup:=True
    ActiveWorkbook.Close
    
    Application.DisplayAlerts = True
    
    End Sub
    

    It's making a copy of the entire strSourceSheet sheet, which opens a new workbook, which we can then save as a .csv file, then it closes the newly saved .csv file, not messing up file name on your original file.

    0 讨论(0)
  • 2020-12-10 11:51

    You just need to save the workbook as a CSV file. Excel will pop up a dialog warning that you are saving to a single sheet, but you can suppress the warning with Application.DisplayAlerts = False.

    Don't forget to put it back to true though.

    0 讨论(0)
  • 2020-12-10 11:51

    This is fairly generic

    Sub WriteCSVs()
    
    Dim mySheet As Worksheet
    Dim myPath As String
    
    'Application.DisplayAlerts = False
    
    For Each mySheet In ActiveWorkbook.Worksheets
    
        myPath = "\\myserver\myfolder\"
    
        ActiveWorkbook.Sheets(mySheet.Index).Copy
        ActiveWorkbook.SaveAs Filename:=myPath & mySheet.Name, FileFormat:=xlCSV, CreateBackup:=True
        ActiveWorkbook.Close
    
    Next mySheet
    
    'Application.DisplayAlerts = True
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题