![](/img/trans.png)
[英]Deleting an Excel Row if the year of the cell value is less than a specific year
[英]Deleting a Row in Excel if Value is less than X
我試圖在excel中刪除符合條件的行,該行滿足列B中的值小於100的條件。我看到了並嘗試遵循它,但是我對VBA並不了解,所以我不是確定要去哪里。
編輯這是到目前為止我嘗試過的代碼,它不起作用。
Sub DeleteR()
Dim myRow As Range
Dim toDelete As Range
For i = 2 To 5000
If Worksheets("PalCountPLTZR").Cells(i, 2) < "99" Then
Set myRow = Worksheets("PalCountPLTZR").Rows(i)
If toDelete Is Nothing Then
Set toDelete = myRow
Else
Set toDelete = Union(toDelete, myRow)
End If
End If
Next i
End Sub
我不確定為什么它不起作用,但是我想知道它查看的單元格是否來自查詢。
您可以使用此循環來檢查和刪除“ A”列中的行:
Sub DeleteRows()
'get last row in column A
Last = Cells(Rows.Count, "A").End(xlUp).Row
For i = Last To 1 Step -1
'if cell value is less than 100
If (Cells(i, "A").Value) < 100 Then
'delete entire row
Cells(i, "A").EntireRow.Delete
End If
Next i
End Sub
Ed在鏈接的帖子中向您展示了.Delete部分。
克里斯·尼爾森(Chris Nielsen)的解決方案很簡單,並且效果很好。 稍微短一點的選擇是...
ws.Rows(Rand).Delete
除了這些知識之外,您還需要添加一個if語句以顯示要刪除的條件,例如:
If Cells(i,2)="x" Then
Rows(i).Delete
End If
這種方法將為i使用循環,因此您需要對此進行閱讀。
祝您好運,並在將來提供您嘗試過的代碼,尤其是在網上找到其他代碼后如何嘗試實現的代碼,因此我們可以嘗試幫助解決問題。
兩件事情,
"99"
應該只是99
而且您永遠不會在最后刪除。
Sub DeleteR()
Dim myRow As Range
Dim toDelete As Range
For i = 2 To 5000
If Worksheets("PalCountPLTZR").Cells(i, 2) < 99 Then
Set myRow = Worksheets("PalCountPLTZR").Rows(i)
If toDelete Is Nothing Then
Set toDelete = myRow
Else
Set toDelete = Union(toDelete, myRow)
End If
End If
Next i
If Not toDelete Is Nothing Then toDelete.EntireRow.Delete
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.