我有一個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