使用嵌套的if语句将XLSX工作表(多个)导出为单独的CSV表

时间:2015-10-05 14:16:32

标签: excel vba excel-vba csv

我正在尝试将15个工作表从Excel工作簿导出到CSV文件。该工作簿共包含18张。前三张表不需要导出,因为它们包含原始数据,指令和计算。

生成的CSV文件用于导入Microsoft Dynamics AX。我需要清除任何含有"〜"在保存为csv之前。但只能出口15张。

我在这个论坛上找到了多个线程,允许我成功创建一个csv。

我的VBA最终看起来像这样:(将一些代码集合在一起)

Sub SaveWorksheetsAsCsv()
Dim WS As Excel.Worksheet
Dim SaveToDirectory As String
Dim CurrentWorkbook As String
Dim CurrentFormat As Long
CurrentWorkbook = ThisWorkbook.FullName
CurrentFormat = ThisWorkbook.FileFormat
' Store current details for the workbook
SaveToDirectory = "C:\Temp\Prices\CSV\"
For Each WS In ThisWorkbook.Worksheets
If WS.Name <> "Instructions" And WS.Name <> "Parameters" And WS.Name <> "BI Data & Worksheet" Then

    Sheets(WS.Name).Copy
    For Each Cell In [b:b]
    If Cell.Value = "~" Then Cell.ClearContents
    'put any value you want here
    Next Cell
    Sheets(WS.Name).Copy
    For Each Cell In [A:A]
    If Cell.Value = "~" Then Cell.ClearContents
    'put any value you want here
    Next Cell


    ActiveWorkbook.SaveAs Filename:=SaveToDirectory & WS.Name & ".csv", FileFormat:=xlCSV
    ActiveWorkbook.Close SaveChanges:=False
    ThisWorkbook.Activate
End If
Next
Application.DisplayAlerts = False
ThisWorkbook.SaveAs Filename:=CurrentWorkbook, FileFormat:=CurrentFormat
Application.DisplayAlerts = True
' Temporarily turn alerts off to prevent the user being prompted
'  about overwriting the original file.
End Sub

我遇到的问题是,虽然导出运行正常,而且CSV看起来很棒,但所有临时的&#34;复制&#34; ActiveWorkbook.Close SaveChanges:=False声明不再关闭工作簿。

我确信这与我的嵌套IF语句有关,但无法弄清楚我做错了什么。

2 个答案:

答案 0 :(得分:1)

完成后,您可以手动关闭它们。

    Dim wb As Workbook

    Application.DisplayAlerts = False

    'Loop though each workbook
    For Each wb In Application.Workbooks

        'Activate the workbook
        wb.Activate
        if wb.name = "something you want to close" then
            wb.Close
        end if

    Next wb

    Application.DisplayAlerts = True

编辑:有关原始问题的更多信息。见评论

For Each WS In ThisWorkbook.Worksheets
If WS.Name <> "Instructions" And WS.Name <> "Parameters" And WS.Name <> "BI Data & Worksheet" Then

    'By doing this copy, Excel is opening an additional workbook.
    'Let's call it "Book1"
    'Book1 is now active
    Sheets(WS.Name).Copy
    For Each Cell In [b:b]
    If Cell.Value = "~" Then Cell.ClearContents
        'put any value you want here
    Next Cell

    'By doing this copy, Excel is opening an additional workbook.
    'Let's call it "Book2".
    'Book2 is now active. Book1 is no longer active.
    Sheets(WS.Name).Copy
    For Each Cell In [A:A]
    If Cell.Value = "~" Then Cell.ClearContents
         'put any value you want here
    Next Cell

    'This is working on the active workbook "Book2"
    'Book1 is no longer active so it will not get saved or closed.
    ActiveWorkbook.SaveAs Filename:=SaveToDirectory & WS.Name & ".csv", FileFormat:=xlCSV
    ActiveWorkbook.Close SaveChanges:=False
    ThisWorkbook.Activate
End If
Next

如果您只复制一次,我认为它会按照您想要的方式工作。

如果要将工作表复制到同一工作簿中,则必须使用After:= Sheets(“SheetName”)参数。那你就不用担心关闭书了。

Sheets(WS.name).Copy After:=Sheets("SomeSheetName") 
ActiveSheet.Name = "SomeNewSheetName"

希望有所帮助。

答案 1 :(得分:0)

谢谢!现场!

我删除了第二张ws表格副本,一切都按预期工作:

Sheets(ws.Name).Copy
For Each Cell In [b:b]
If Cell.Value = "~" Then Cell.ClearContents
'put any value you want here
Next Cell
For Each Cell In [A:A]
If Cell.Value = "~" Then Cell.ClearContents
'put any value you want here
Next Cell

在旁注中:我导出到CSV的工作表具有公式(嵌套if语句)下降到第100,000行。如果满足某些参数,则在另一个工作表上查找另一个cel来填充单元格。

最初如果参数不符合,那么我正在显示&#34;&#34;或无。导出为CSV时,这些单元格用逗号填充。我想因为那里有一个公式。

通过显示&#34;〜&#34;作为公式的结果,我可以使用上述VBA在导出之前完全清除单元格。

csv导出中包含公式但不包含内容的单元格中的逗号是很多人在此问题上根据论坛帖子数量来判断的问题。我希望这个技巧可以帮助别人。