我有一个vbscript,将特定范围的行转换为csv文件。
我的问题是它也复制空行而不需要蓝色行。如何在复制之前删除这些完整的空行或将它们从复制中排除?
我的代码:从xlsx删除蓝色和空单元格与vbscript
Public Sub xlsToCsv()
Const WorkingDir = "C:\Test\"
Const xlCSV = 24
Const xlUp = -4162
Dim fso, SaveName, myFile
Dim objExcel, objWorkbook, wsSource, wsTarget
myFile = "source_file.xlsx"
SaveName = "test.csv"
With CreateObject("Scripting.FilesystemObject")
If Not .FileExists(WorkingDir & myFile) Then
MsgBox "File not found:" & vbCrLf & WorkingDir & myFile, vbInformation, "Script Cancelled"
WScript.Quit
End If
End With
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.DisplayAlerts = False
Set objWorkbook = objExcel.Workbooks.Open(WorkingDir & myFile)
Set wsSource = objWorkbook.Sheets(1)
Set wsTarget = objWorkbook.Sheets.Add()
With wsTarget
.Cells(1,1).Value = "ID"
.Cells(1,2).Value = "NAME"
.Cells(1,3).Value = "DESC"
End With
With wsSource
.Range("F7", .Range("F" & .Rows.Count).End(xlUp)).Copy wsTarget.Range("A2")
.Range("A7", .Range("A" & .Rows.Count).End(xlUp)).Copy wsTarget.Range("B2")
.Range("E7", .Range("E" & .Rows.Count).End(xlUp)).Copy wsTarget.Range("C2")
End With
objWorkbook.SaveAs WorkingDir & SaveName, xlCSV
objWorkbook.Close True
Set objWorkbook = Nothing
Set objExcel = Nothing
Set fso = Nothing
Set myFolder = Nothing
End Sub
call xlsToCsv()
您可以自动筛选空白或蓝色行并删除它们。然后制作你的CSV。 – danieltakeshi
我不仅需要细胞。如果整行是空的,我需要删除一行。我可以过滤吗?我怎样才能过滤蓝色细胞? – nolags
请参阅以下问题:[用于彩色过滤](https://stackoverflow.com/a/35982191/7690982)和[删除空白行](https://stackoverflow.com/a/22542280/7690982)或[VBA代码删除一列基于列中非空单元格](https://stackoverflow.com/a/26610471/7690982) – danieltakeshi