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. I'd like it to stop at 147 or whatever number of rows throws the filter.
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. 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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.