我有一個宏,可以從資料集中過濾和洗掉不必要的資料并創建資料透視表。過濾和洗掉資料的代碼如下:
For i = 2 To LastRowG
If Range("G" & i).Value = "APGCVGP" Then
lo.Range.autofilter Field:=7, Criteria1:="APGCVGP"
Application.DisplayAlerts = False
lo.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = True
lo.autofilter.ShowAllData
Else
End If
Next i
如何使用這種格式洗掉某一列中包含空白單元格的行?
我嘗試了以下方法,但沒有任何反應:
For i = 2 To LastRowG
If Range("G" & i).Value = "=" Then
lo.Range.autofilter Field:=7, Criteria1:="="
Application.DisplayAlerts = False
lo.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = False
lo.autofilter.ShowAllData
Else
End If
Next i
我嘗試了相同的代碼,但使用"<>"了 代替"=",但這也沒有做任何事情。
uj5u.com熱心網友回復:
而不是你的第一個程序中的回圈,你不能像這樣避免它嗎?
If WorksheetFunction.CountIf(Range("G:G"),"APGCVGP") Then
lo.Range.autofilter Field:=7, Criteria1:="APGCVGP"
Application.DisplayAlerts = False
lo.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = True
lo.autofilter.ShowAllData
End If
對于您的第二個程式,您將使用
If Range("G" & i).Value = "" Then
但是,同樣,我認為您不需要回圈,即如果 G2 為空白,那么您將洗掉所有空白單元格,但是,在回圈的下一次迭代中,如果 G3 也為空白,您將嘗試再次洗掉您已經洗掉的單元格...無論如何,您不需要過濾器來識別空白,即
lo.DataBodyRange.Columns(7).SpecialCells(xlCellTypeBlanks).Rows.Delete
uj5u.com熱心網友回復:
如果您只想根據 g 列中的值為空白或等于 '' 洗掉整行,那么這應該有效。請注意,在最后洗掉行而不是在回圈時洗掉行要高效得多,也是更好的做法。
在運行任何代碼之前小心保存您的作業,因為我們看不到您真正在做什么。
Sub deleteSomeRows()
Dim i As Long, killRange As Range, aCell As Range
For i = 2 To LastRowG
Set aCell = Range("G" & i)
If aCell.Value = "" Then
If killRange Is Nothing Then
Set killRange = aCell.EntireRow
Else
Set killRange = Union(killRange, aCell.EntireRow)
End If
End If
Next i
If Not killRange Is Nothing Then
killRange.ClearContents
killRange.Delete (xlUp)
End If
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/471035.html
