0

我正在尝试循环工作表中的所有非隐藏和非空白行。问题是,它确实会遍历非隐藏的表单,但是当它到达非隐藏表单的末尾时,它会一直持续下去。

我知道它在过滤器之后检索了 147 行(包括隐藏行),并且在执行循环时确实跳过了它们,但是在完成 147 之后,它继续到 148 等等,我有一个验证来检查它是否检索到是空白的,它停在那里。我希望它停在 147 或任何数量的行抛出过滤器。

我在做什么:

'I need the cells in B1 to begin with 0 or W, I guess I'd have to add a non-blank option (see below)

    With MaterialListSheet
        .AutoFilterMode = False
        .Range("B1").AutoFilter Field:=1, Criteria1:="0*", Operator:=xlOr, Criteria2:="W*"
    End With

    Set rangeMaterialList = MaterialListSheet.Range("B2:B" & Rows.Count)

 For Each CellML In rangeMaterialList.SpecialCells(xlCellTypeVisible)

       'Reset the variable in order to avoid unwanted codes
        BomCodesToSplit = ""
       'Check if there's a code in the cell
        If MaterialListSheet.Range("C" & CellML.Row & ":C" & CellML.Row).Value <> "" Then
            BomCodesToSplit = MaterialListSheet.Range("C" & CellML.Row & ":C" & CellML.Row).Value
        End If
        'Check if theres a code in the cell AND if it does concatenate (in case the variable isn't empty)

        If MaterialListSheet.Range("D" & CellML.Row & ":D" & CellML.Row).Value <> "" Then
           If BomCodesToSplit <> "" Then
                BomCodesToSplit = BomCodesToSplit & " / " & MaterialListSheet.Range("D" & CellML.Row & ":D" & CellML.Row).Value
            Else
                BomCodesToSplit = MaterialListSheet.Range("D" & CellML.Row & ":D" & CellML.Row).Value
           End If
        End If

        'Check if any BomCodes were retrieved
        If BomCodesToSplit = "" Then
           MsgBox "There was an error getting the Bom Codes", vbCritical, "Execution Terminated"
           End
        Else
            BomCodes = Split(BomCodesToSplit, "/")
        End If

   Next CellML
End Sub

我试过的过滤器:

'Im aware this doesn't work but decided to try it anyway
    With MaterialListSheet
        .AutoFilterMode = False
        .Range("B1").AutoFilter Field:=1, Criteria1:="<>"
        .Range("B1").AutoFilter Field:=1, Criteria1:="0*", Operator:=xlOr, Criteria2:="W*"
    End With

'我也意识到这是不可能的(我认为)

With MaterialListSheet
    .AutoFilterMode = False
    .Range("B1").AutoFilter Field:=1, Criteria1:="0*", Operator:=xlOr, Criteria2:="W*", Operator:=xlAnd, Criteria3:="<>"
End With

I don't even know if it's a filter thing, or maybe i'm missing something? I'm new to VBA and there's MANY things I don't know yet. All I want is to loop until (147 in this case, so it doesn't enter into the 'Check if any BomCodes were retrieved code.

4

1 回答 1

2

Remove this line:

Set rangeMaterialList = MaterialListSheet.Range("B2:B" & Rows.Count)

and before any filtering is applied, insert:

Dim N As Long
N = Cells(Rows.Count, "B").End(xlUp).Row
Set rangeMaterialList = MaterialListSheet.Range("B2:B" & N)

EDIT #1:

The coding you posted went too far down column B. Nothing limited it to the table being filtered. Without a limit, SpecialCells would all the way down to the bottom of UsedRange.

The change I suggested limited the range down to the bottom of data in column B.

The reason I specified it be done before the filter is applied is that End(xlup) may not work on filtered data.

于 2013-10-30T14:52:10.300 回答