遍历命名范围并选择行和列

时间:2019-08-01 17:00:34

标签: excel vba

我有一个命名范围,如下所示:

named range

对于第2列等于零的每一行,我想从A:F列(六列)中清除该行。我所拥有的不起作用,因为它选择了整个命名范围,并且当if语句变为true时,整个过程变白了。

Sub modFinishFinancialEstimate()

Dim myrange As Range
Dim ws As Worksheet

Set myrange = Range("actual_cost_of_svc")
Set ws = ActiveSheet

ws.Select

For i = myrange.Rows(1).row To myrange.Rows.Count
    MsgBox "The Count of services is " & Cells(i, 2).Value
    If Range("B" & i).Value = 0 Then
        MsgBox "The value is " & Cells(i, 2).Value & " and will be whited out"
        For Each col In myrange.Columns
            With Selection.Font
                .ThemeColor = xlThemeColorDark1
                .TintAndShade = 0
            End With
            ActiveWorkbook.ws.Sort.SortFields.Add Key:=Range( _
    myrange), SortOn:=xlSortOnCellColor, Order:=xlAscending, DataOption:= _
    xlSortNormal
            With Selection.Sort
                .SetRange myrange
                .Header = xlGuess
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
        Next col
    End If
Next

End Sub

我在上面的代码中遇到的问题是,它仅检查第一行,然后退出子程序。

1 个答案:

答案 0 :(得分:0)

第一行将是

msgbox myrange.rows(1).row

You don't need to Select anything

或者,您可以使循环相对,即i的第myrange个单元,而不是工作表的第i个单元。