从Excel中创建CSV文件时删除空白单元格

时间:2016-06-09 15:21:32

标签: excel vba excel-vba csv

我正在尝试从excel创建多个csv文件。我有一个vba,每个选项卡创建一个csv文件,但输出中有空格。

我有6列csv文件返回有没有办法让我忽略空格,所以我没有空白的逗号,例如“,”,

以下是我正在使用的代码:

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 = "H:\test\"
For Each WS In ThisWorkbook.Worksheet
Sheets(WS.Name).Copy
ActiveWorkbook.SaveAs Filename:=SaveToDirectory & ThisWorkbook.Name & "-" & WS.Name & ".csv", FileFormat:=xlCSV
ActiveWorkbook.Close savechanges:=False
ThisWorkbook.Activate
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

谢谢乔

1 个答案:

答案 0 :(得分:-1)

可以找到空白单元格Selection.SpecialCells(xlCellTypeBlanks).Select,但我没有看到您选择任何数据。

如果您只需要某些列,我建议您在复制到新工作表后删除其他列。在Copy

之后添加此行
ActiveSheet.Columns(X).Delete

对要删除的每个列执行此操作,将X替换为列的名称,即。 “C”。