Excel Macro Multiple Sheets to CSV

后端 未结 2 1073
夕颜
夕颜 2020-12-12 07:06

I have a macro that I am running in Excel to separate 49 sheets into individual CSV files.

However, it is getting caught up on line 7

Application.A         


        
2条回答
  •  不思量自难忘°
    2020-12-12 07:56

    Consider this.

    Sub test()
    
        Dim ws As Worksheet
        Dim GetSheetName As String
    
        For Each ws In ThisWorkbook.Worksheets
            If ws.Name <> "Sheet1" Then ' Assuming there is one sheet that you DON'T want to save as a CSV
    
            ws.Select
            GetSheetName = ActiveSheet.Name
                Set shtToExport = ActiveSheet     ' Sheet to export as CSV
                    Set wbkExport = Application.Workbooks.Add
                    shtToExport.Copy Before:=wbkExport.Worksheets(wbkExport.Worksheets.Count)
                    Application.DisplayAlerts = False       ' Possibly overwrite without asking
                    wbkExport.SaveAs Filename:="C:\your_path_here\Desktop\" & GetSheetName & ".csv", FileFormat:=xlCSV
                    Application.DisplayAlerts = True
                    wbkExport.Close SaveChanges:=False
    
            End If
        Next ws
    
    End Sub
    

提交回复
热议问题