[英]Delete entire row if cell contains the string X
這不一定是 VBA 任務 - 使用自動過濾器最容易解決此特定任務。
1.插入自動過濾器(在Excel 2010中點擊主頁->(編輯)排序和過濾器->過濾器)
2. 篩選“網站”列
3. 標記“無”並刪除它們
4. 清除過濾器
好的,我知道這適用於 VBA,但如果您需要一次性批量刪除,您可以使用以下 Excel 功能: http : //blog.contextures.com/archives/2010/06/21/fast-way-to -find-and-delete-excel-rows/希望這對任何人都有幫助
查找字符串“paper”的示例:
在“開發人員選項卡”中,轉到“Visual Basic”並創建一個模塊。 復制粘貼以下內容。 記住更改代碼,這取決於您想要什么。 然后運行模塊。
Sub sbDelete_Rows_IF_Cell_Contains_String_Text_Value()
Dim lRow As Long
Dim iCntr As Long
lRow = 390
For iCntr = lRow To 1 Step -1
If Cells(iCntr, 5).Value = "none" Then
Rows(iCntr).Delete
End If
Next
End Sub
lRow : 放入當前文件的行數。
"If" 中的數字 "5" 用於第五 (E) 列
這是在另一條評論中提到的,但你可以嘗試這樣的事情。
Sub FilterAndDelete()
Application.DisplayAlerts = False
With Sheet1 'Change this to your sheet name
.AutoFilterMode = False
.Range("A3:K3").AutoFilter
.Range("A3:K3").AutoFilter Field:=5, Criteria1:="none"
.UsedRange.Offset(1, 0).Resize(ActiveSheet.UsedRange.Rows.Count - 1).Rows.Delete
End With
Application.DisplayAlerts = True
End Sub
我還沒有測試過這個,它來自內存,所以它可能需要一些調整,但它應該在不循環數千行的情況下完成工作。 您需要刪除 11-Jul 以便UsedRange
正確或將偏移量更改為 2 行而不是.Offset(1,0)
的 1 行。
通常,在我執行.Delete
之前,我將使用.Select
而不是 Delete 來運行宏,這樣我就可以確定正確的范圍將被刪除,這可能值得檢查以確保刪除了適當的范圍。
試試這個 ...
Dim r as Range
Dim x as Integer
For x = 5000 to 4 step -1 '---> or change as you want //Thanx 4 KazJaw
set r = range("E" & format(x))
if ucase(r.Value) = "NONE" then
Rows(x).EntireRow.Delete
end if
Next
把它放在一個宏中(它會檢查 75000 行,如果你願意,你可以降低這個數字
Columns("E:E").Select Selection.AutoFilter ActiveSheet.Range("$E$1:$E$75000").AutoFilter Field:=1, Criteria1:="none" Range("E2:E75000").SpecialCells(xlCellTypeVisible).Select Selection.EntireRow.Delete ActiveSheet.Cells.EntireRow.Hidden = False ActiveSheet.Range("$E$1:$E$75000").AutoFilter Field:=1 Columns("E:E").Select Selection.AutoFilter Range("E2").Select Range("A1").Select
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.