Vba 删除其中的行;H:H";具有正值

Vba 删除其中的行;H:H";具有正值,vba,excel,Vba,Excel,排序后,我需要删除H列中正值开始的行。我试图使用查找功能。我知道>0不起作用,但不确定从这里到哪里。如果我知道如何向上选择和删除,我可以将排序切换为降序并搜索“-” Source_Workbook.Worksheets("Sheet1").Activate Source_Workbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("H:H"), SortOn:=xlSortOnValues, Order:=xlAscendin

排序后,我需要删除H列中正值开始的行。我试图使用查找功能。我知道>0不起作用,但不确定从这里到哪里。如果我知道如何向上选择和删除,我可以将排序切换为降序并搜索“-”

    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

试试这个,在范围内的每个单元格中循环。仔细检查my
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

这似乎只适用于一个领域


我添加了一个,以防您希望继续进行排序的输出。

如果按升序排序,则
匹配(1E-99,H:H)
可以很快找到包含最后一个非正数的行。向下偏移1行,然后将所有东西都抓到底部应该很快。@Jeeped-从没想过,谢谢你的提示!谢谢@BruceWayne,我首先尝试了这个,因为它似乎更接近我的编码风格,但最后我被卡住了。它不会执行第二个with语句。我对vba相当陌生,所以不知道为什么。我尝试了许多不同的调整,但都不起作用。@chefAna-我想知道这是否是我在声明
myWS
时在
“Sheet1”
中意外使用的引号类型。这些应该是直截了当的引语。哦,很高兴吉普得到了它!不,我使用了通用的“Sheet1”来保持文档的完整性,所以我没有复制或粘贴任何东西。它位于第二行,语句为:If.cells(i,8).Value>0 Then.Rows(i).EntireRow.delete非常感谢您的帮助!它工作得非常好,我将能够在其他报告中使用此方法。
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