excel - 创建 .CSV 文件时消除空白行

标签 excel vba

我正在尝试创建一个宏来创建包含输入的第一行到最后一行数据的 .CSV 文件。当我运行宏时,.CSV 文件中显示空白行。我需要什么代码来消除写入文件的空白行?下面列出了 VBA 代码和 .CSV 文件中的数据快照。任何帮助将不胜感激。

' DeleteHeadings
Rows("1:1").Select
Selection.DeleteShift:=xlUp


'Find thelast Row with data in a Column
DimLastRow As Long
DimFirstRow As Integer

With ActiveSheet
LastRow =.Cells(.Rows.Count, "A").End(xlUp).Row
End With


FirstRow= 1


'SelectRows to Save
Range(Cells(FirstRow,"A"), Cells(LastRow, "BH")).Select

' SaveActive Workbook As CSV
Application.DisplayAlerts= False
ActiveWorkbook.SaveAsFilename:="Z:\RDS\Towers_Watson_Trans_File1", FileFormat:=xlCSV,CreateBackup:=False
ActiveWorkbook.CloseSaveChanges:=True
Application.DisplayAlerts= True

.CSV 文件中的数据

444444444,,HILDA,,ZOLDAN,19490611,F,{92B32AFF-7A9C-4FB0-BC98-51CBA0F642CC},20150801,1800.00,M,20150801,,,,,,,,,,,,,,,,,,,,,915 
444444444,,MARY,,SHANAHAN,19501111,M,{92B32AFF-7A9C-4FB0-BC98-51CBA0F642CC},20151101,1080.00,M,20151101,,,,,, , , , , ,,,,,,,, 
444444444,,MARY,,SZCZESNIAK,19450923,F,{92B32AFF-7A9C-4FB0-BC98-51CBA0F642CC},20150801,1800.00,M,20150801,,,, , , , , , , ,,,, 
444444444,,DORIS,,HAMMONDS,19461214,F,{92B32AFF-7A9C-4FB0-BC98-51CBA0F642CC},20150801,1800.00,M,20150801,,,,,,,,,,,,,,,,,,,,,37 
666666666,,ROSA,,GANA,19530613,F,{92B32AFF-7A9C-4FB0-BC98-51CBA0F642CC},20180601,1080.00,M,20180601,,,,,,,,,,,,,,,,,,,,,71SACK 
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

最佳答案

.SaveAs 将保存工作表,而不是选择内容(我没有看到任何 options 来保存选择内容而不是整个工作表)。数据末尾有空白行,因此它们将保存到 .CSV 中。

尝试删除末尾的空白行:

' DeleteHeadings
Rows("1:1").Select
Selection.DeleteShift:=xlUp

'Find thelast Row with data in a Column
Dim LastRow As Long
'Dim FirstRow As Integer       'No longer needed - at least for this snippet

LastRow = ActiveSheet.Cells(.Rows.Count, "A").End(xlUp).Row
Rows(LastRow + 1 & ":" & ActiveSheet.UsedRange.Rows.Count).Delete

'SelectRows to Save
'you can comment these 2 lines out vvvv  They're not buying you anything
FirstRow= 1
Range(Cells(FirstRow,"A"), Cells(LastRow, "BH")).Select

' SaveActive Workbook As CSV
Application.DisplayAlerts= False
ActiveWorkbook.SaveAsFilename:="Z:\RDS\Towers_Watson_Trans_File1", _
      FileFormat:=xlCSV, CreateBackup:=False
ActiveWorkbook.Close SaveChanges:=True
Application.DisplayAlerts= True

关于excel - 创建 .CSV 文件时消除空白行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30261586/

相关文章:

delphi - 从 Delphi 修改 Excel 形状

vba - .Select、.Activesheet、.Activecell 等...

excel - 将文件从多个文件夹移动到单个文件夹

Excel VBA,错误 438 "object doesn' 不支持此属性或方法

excel - 使用 MSBuild 更改 .xla 文件

python - 用于分隔 Excel 工作表的多个数据框列表

vba - 如何在VBA中获取CSV文件中数据的行数

vba - 如何通过指定范围而不是UsedRange选项使用Excel宏清除单元格填充颜色

excel - Powershell Excel ComObject 打开自定义分隔符

c++ - exe文件在C++中不是内部或外部命令,也不是可运行的程序或批处理文件