使用宏将 Office Excel 表导出为 csv

使用宏将 Office Excel 表导出为 csv

我在 Excel 2010 中使用 Excel 表格。我想快速将这些表格的内容导出为 *.csv。

我当前的工作流程:1. 手动选择表格 2. 将内容复制到新工作簿中 3. 将工作簿保存为 *.csv 文件

所需工作流程:1. 手动选择表格 2. 运行写入预定义文件名的宏

由于表具有唯一的名称(例如 CurrentDataTable),是否存在一个函数可以采用表名、目标文件和所需的输出格式并写入输出文件?

答案1

没有内置的 Excel 命令或函数可以执行您想要的操作,但您可以使用 VBA 对其进行编程。

以下代码可能接近您要查找的内容:

Sub ExportTable()

    Dim wb As Workbook, wbNew As Workbook
    Dim ws As Worksheet, wsNew As Worksheet
    Dim wbNewName As String


   Set wb = ThisWorkbook
   Set ws = ActiveSheet

   Set wbNew = Workbooks.Add

   With wbNew
       Set wsNew = wbNew.Sheets("Sheet1")
       wbNewName = ws.ListObjects(1).Name
       ws.ListObjects(1).Range.Copy
       wsNew.Range("A1").PasteSpecial Paste:=xlPasteAll
       .SaveAs Filename:=wb.Path & "\" & wbNewName & ".csv", _
             FileFormat:=xlCSVMSDOS, CreateBackup:=False
   End With

End Sub

代码假设每个工作表中都有一个表。它会创建一个新工作簿,将表复制到该工作簿的 Sheet 1 中,并将工作簿保存为与表同名的 CSV 文件。

答案2

这是我对 Excel 2013 的 chuff 答案的版本。它还禁用了模式对话框:

Sub ExportCSV()

   Dim wb As Workbook, wbNew As Workbook
   Dim ws As Worksheet, wsNew As Worksheet

   Set wb = ThisWorkbook
   Set ws = ActiveSheet

   Set wbNew = Workbooks.Add
   Application.DisplayAlerts = False
   With wbNew
       Set wsNew = wbNew.Sheets("Sheet1")
       ws.Rows.Copy
       wsNew.Paste
       .SaveAs Filename:=ws.name & ".csv", FileFormat:=xlCSV, CreateBackup:=True
       wsNew.Delete
   End With
   Windows(ws.name & ".csv").Activate
   ActiveWindow.Close
   Application.DisplayAlerts = True

End Sub

答案3

我需要做同样的事情,但需要指定哪些表必须导出为 CSV 文件。我创建了一个名为“ExportTables”的范围,VBA 如下:

Public Sub Export()
    Dim i       As Integer
    Dim iMax    As Integer
    Dim sTable  As String

    iMax = Range("ExportTables").Rows.Count

    For i = 1 To iMax
        sTable = Range("ExportTables").Cells(i, 1).Value
        Call ExportTable(sTable)
    Next i
End Sub

Public Sub ExportTable(tableName As String)
    Dim wkb         As Workbook
    Dim wkbNew      As Workbook
    Dim wks         As Worksheet
    Dim wksNew      As Worksheet

    Set wkb = ThisWorkbook

    Application.Goto Reference:=tableName
    Set wks = ActiveSheet

    Set wkbNew = Workbooks.Add
    Set wksNew = wkbNew.Sheets(1)

    wks.ListObjects(tableName).Range.Copy
    wksNew.Range("A1").PasteSpecial Paste:=xlPasteValues

    Application.DisplayAlerts = False

    wkbNew.SaveAs Filename:=wkb.Path & "\" & tableName & ".csv", _
        FileFormat:=xlCSV, CreateBackup:=False
    wkbNew.Close SaveChanges:=False

    Application.DisplayAlerts = True

    ' Release object variables.
    Set wkb = Nothing
    Set wkbNew = Nothing
    Set wks = Nothing
    Set wksNew = Nothing
End Sub

答案4

我收到 1004 错误,原因是上面的代码在 Thorsten 的代码中删除了。我将其与这个例子并得到了这个,对我有用:

Sub ExportCSV2()

   Dim wb As Workbook, wbNew As Workbook
   Dim ws As Worksheet, wsNew As Worksheet

   Set wb = ThisWorkbook
   Set ws = ActiveSheet

   Application.DisplayAlerts = False

   ws.Copy
   ActiveWorkbook.SaveAs Filename:=ws.Name & ".csv", FileFormat:=xlCSV, CreateBackup:=True
   Windows(ws.Name & ".csv").Activate
   ActiveWorkbook.Close False
   Application.DisplayAlerts = True

End Sub

请注意,这种(Thorsten)方法导出的是整张表,而不仅仅是表格,因此您会得到很多空行。

相关内容