简体   繁体   English

sheet.SpecialCells(xlCellTypeVisible)中的范围也选择了空白行,尽管应用了VBA过滤器

[英]range in sheet.SpecialCells(xlCellTypeVisible) also selecting blank rows although a filter was applied VBA

I'm trying to loop all the non-hidden and non-blank rows in a sheet. 我正在尝试循环一个工作表中的所有非隐藏和非空白行。 The thing is, it does loop through the non-hidden but when it reaches the end of the non-hidden sheets, it just keeps going forever. 问题是,它确实循环通过非隐藏,但当它到达非隐藏表的末尾时,它就会永远地继续前进。

I know it retrieves 147 rows after the filter (including hidden rows) and it does skip them when doing the loop but, after it's done with the 147, it continues to 148 and so on, I have a validation to check if what it retrieved was blank and it stops there. 我知道它在过滤器后检索了147行(包括隐藏的行),并且在执行循环时它确实跳过了它们,但是在完成147之后,它继续到148,依此类推,我有一个验证来检查它是否检索到空白,它停在那里。 I'd like it to stop at 147 or whatever number of rows throws the filter. 我希望它停在147或任何数量的行抛出过滤器。

What I'm doing: 我在做什么:

'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

The Filters I've tried: 我试过的过滤器:

'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

'I also realize this is not possible (I think) '我也意识到这是不可能的(我认为)

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. 我是VBA的新手,还有很多我还不知道的事情。 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. 我想要的只是循环直到(在这种情况下为147,因此它不会进入'Check if any BomCodes were retrieved代码。

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 : 编辑#1

The coding you posted went too far down column B. Nothing limited it to the table being filtered. 您发布的编码在B列中走得太远。没有任何限制它被过滤的表格。 Without a limit, SpecialCells would all the way down to the bottom of UsedRange. 没有限制,SpecialCells将一直到UsedRange的底部。

The change I suggested limited the range down to the bottom of data in column B. 我建议的更改将范围限制在B列中的数据底部。

The reason I specified it be done before the filter is applied is that End(xlup) may not work on filtered data. 应用过滤器之前指定它的原因是End(xlup)可能不适用于过滤数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尽管使用了`SpecialCells(xlCellTypeVisible)`,但选择可见单元格不起作用 - Selecting Visible Cells is not working although using `SpecialCells(xlCellTypeVisible)` VBA:处理过滤的行和SpecialCells(xlCellTypeVisible)与将数据复制到新表中 - VBA: Working with filtered rows and SpecialCells(xlCellTypeVisible) vs copying data into new sheet VBA通过.SpecialCells(xlCellTypeVisible)合并自动过滤的单元格 - VBA merging autofiltered cells via .SpecialCells(xlCellTypeVisible).Range 找到了&#39;TargetTable.Range.SpecialCells(xlCellTypeVisible).Copy _&#39;目标:= Sheets(“ Sheet8”)。Range(“ A1”) - found 'TargetTable.Range.SpecialCells(xlCellTypeVisible).Copy _ ' Destination:=Sheets(“Sheet8”).Range(“A1”) SpecialCells(xlCellTypeVisible)还包括隐藏/过滤的单元格 - SpecialCells(xlCellTypeVisible) also includes hidden/filtered cells 在 excel vba 中选择了 SpecialCells(xlCellTypeVisible) 额外行 - SpecialCells(xlCellTypeVisible) extra row is selected in excel vba 无法获取xlcelltypevisible的范围类的Specialcells属性 - unable to get the Specialcells property of the range class for xlcelltypevisible SpecialCells(xlCellTypeVisible) - SpecialCells(xlCellTypeVisible) 遍历过滤器中的枢轴项时,vba SpecialCells(xlCellTypeVisible)无法正常工作 - vba SpecialCells(xlCellTypeVisible) not working correctly when looping through pivot items in a filter 将过滤后的范围放入数组 - ....AutoFilter.Range.SpecialCells(xlCellTypeVisible).Offset(1) - Get a filtered range into an array - ….AutoFilter.Range.SpecialCells(xlCellTypeVisible).Offset(1)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM