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

我试图从一个Excel工作簿导出15个工作表到CSV文件。 该工作手册总共包含18页。 前三张纸不需要导出,因为它们包含原始数据,说明和计算。

生成的CSV文件用于导入到Microsoft Dynamics AX中。 在保存为csv之前,我需要清除包含“〜”的任何cel。 但只能从15张出口。

我在这个论坛上发现了多个线程,可以让我成功创build一个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看起来不错,所有临时“复制”工作簿不再被ActiveWorkbook.Close SaveChanges:=Falseclosures。closuresActiveWorkbook.Close SaveChanges:=False语句。

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

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

  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”)参数。 那么你将不必担心closures书籍。

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

希望有所帮助。

谢谢! 发现!

我删除了第二个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。 如果满足某些参数,则在另一个工作表中查找另一个单元格。

最初,如果参数不符合,那么我显示“”或什么也没有。 当输出到CSV但是这些单元格填充逗号。 我想是因为那里有一个公式。

通过显示“〜”作为公式的结果,我可以使用上面的VBA完全清除单元格。

一个csv输出的逗号,包含一个格式但没有内容的cel是很多人遇到的问题,由这个论坛的post数量来判断。 我希望这个技巧可以帮助别人。