删除“H:H”为正值的行

我需要在sorting后删除列H中正值开始的行。 我试图使用查找function。 我知道> 0不起作用,但不知道从哪里去。 我可以切换到降序search“ – ”,如果我能弄清楚如何select向上和删除。

Source_Workbook.Worksheets("Sheet1").Activate Source_Workbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("H:H"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal Cells.Select With ActiveWorkbook.Worksheets("Sheet1").Sort .SetRange Range("A1:R65800") .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Columns("H:H").Select Selection.Find(What:=">0", After:=ActiveCell, LookIn:=xlFormulas, LookAt _ :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False).Select Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select Selection.EntireRow.Delete 

这似乎适合单个字段Range.AutoFilter方法 。

 Sub del_positives() With ActiveWorkbook.Worksheets("Sheet1") 'turn off any exiting autofilter If .AutoFilterMode Then .AutoFilterMode = False 'work with the block of cells radiating out from A1 With .Cells(1, 1).CurrentRegion 'apply the positive number filter to column H .AutoFilter field:=8, Criteria1:=">0" 'step off the header row With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0) 'determine if there are cells to delete If CBool(Application.Subtotal(102, .Cells)) Then 'if there are, delete them .SpecialCells(xlCellTypeVisible).EntireRow.Delete End If End With 'remove the filter .AutoFilter 'if you still want to sort, then do so .Cells.Sort Key1:=.Columns(8), Order1:=xlAscending, _ Orientation:=xlTopToBottom, Header:=xlYes End With If .AutoFilterMode Then .AutoFilterMode = False End With End Sub 

我已经添加了一个Range.Sort方法 ,以防您想要继续sorting的输出。

试试这个,循环遍历范围中的每个单元格。 仔细检查一下,我的lastRow是一个准确和可持续的方式来获得项目中的最后一行:

 Sub t() Dim lastRow as Long, i as Long Dim myWS as worksheet Dim mySourceWB as workbook Dim cel as Range Set mySourceWB = Source_Workbook ' you will probably need to tweak this Set myWS = mySourceWB.Worksheets(“Sheet1”) myWS.Sort.SortFields.Add Key:=myWS.Range("H:H"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With myWS.Sort .SetRange Range("A1:R65800") .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With 'myWS.sort With myWS lastRow = .Cells(.Rows.Count,8).End(xlup).Row For i = lastRow to 1 ' change to 2, if you have headers If .cells(i,8).Value > 0 Then .Rows(i).EntireRow.Delete End if Next i End with 'myWS End Sub