[英](VBA) Delete entire row if cell X is less than AND delete entire row if cell Y is less than
[英]VBA Delete entire row if value is less than specified value - deletes wrong rows
我試圖運行一個小的宏來搜索工作表的S列,如果S列中的值<0.501,它將刪除該行。
我目前正在運行的代碼刪除了一些行,但似乎是隨機執行的,而不是基於s中的單元格值。 誰能看到我的錯誤來自哪里?
Sub sort_delete_500cust()
Dim WS_Count As Integer
Dim I, K As Integer
Dim endrow As Long
' Set WS_Count equal to the number of worksheets in the active
' workbook.
WS_Count = Workbooks("Standard.xlsx").Worksheets.count
' Begin the loop.
For I = 1 To WS_Count
With Worksheets(I)
endrow = .Range("a" & .Rows.count).End(xlUp).row ' only works if cells are unmerged
Range("A2:v2" & endrow).Sort _
Key1:=Range("s2"), Order1:=xlDescending 'key is the sort by column
For K = 2 To endrow
If .Cells(K, 19).Value < 0.501 Then
.Range("S" & K).EntireRow.Delete
End If
Next K
End With
Next I
End Sub
干杯!
您需要以相反的順序遍歷您的K
循環。 否則,行將在刪除時被跳過,因為刪除操作會將它們上移,並且您的K
值將在它們之上遞增。
For K = endrow To 2 Step -1
If CDec(.Cells(K, 19).Value) < CDec(0.501) Then
.Range("S" & K).EntireRow.Delete
End If
Next
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.