我尝试运行一个搜索工作表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
干杯!
答案 0 :(得分:1)
您需要以相反的顺序遍历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