有人能给我一些关于如何使用VBA代码过滤/删除空白的见解吗?出于某种原因,当我录制宏来执行此操作时,它不允许使用VBA构建的某些自定义函数来保存它们的值。感谢。
答案 0 :(得分:1)
以下代码将删除所选列中包含空白的行。下面的代码假设您的数据中的第二列正在测试空白。如果您需要其他帮助,请告诉我们。
Sub DeleteBlanks()
Dim rDataToProcess As Range
Set rDataToProcess = Sheet1.Range("A1").CurrentRegion
'Field in the below method refers to the column that is being filtered, so the second colum
rDataToProcess.AutoFilter field:=2, Criteria1:=""
rDataToProcess.Offset(1).Resize(rDataToProcess.Rows.Count).EntireRow.Delete
Sheet1.AutoFilterMode = False
End Sub
答案 1 :(得分:1)
删除空白单元格的替代方法是设置范围,然后使用Range([your range]).SpecialCells(xlCellTypeBlanks).Delete
编辑:如果要删除整行Range([your range]).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
答案 2 :(得分:0)
就像@user3561813所说的那样,请查看此link以了解更复杂的过滤器,例如多个条件等:
Function Prompt {
If ("$($executionContext.SessionState.Path.CurrentLocation)>".Length -le 30) {
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
} Else {
"PS ...\$(Split-Path -Path $executionContext.SessionState.Path.CurrentLocation -Leaf)$('>' * ($nestedPromptLevel + 1)) ";
} # End If.
} # End Function: Prompt.
例如,上面的代码过滤器同时包含“空白”和“ XXXX”字段
答案 3 :(得分:0)
Sub Blank_Cells_Filter()
'Apply filters to include or exclude blank cells
Dim lo As ListObject
Dim iCol As Long
'Set reference to the first Table on the sheet
Set lo = Sheet1.ListObjects(1)
'Set filter field
iCol = lo.ListColumns("Product").Index
'Blank cells – set equal to nothing
lo.Range.AutoFilter Field:=iCol, Criteria1:="="
'Non-blank cells – use NOT operator <>
lo.Range.AutoFilter Field:=iCol, Criteria1:="<>"
End Sub