Excluding Header Row In Looping Through Filtered Cells

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,570
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
So I found, and tried to adapt this code, that will step through the visible cells of a filtered worksheet to provide me with their row numbers.
It works, but it's including the header row in it's process. I only need the data rows below row 2.
If I try to reset the filter_rng set earlier from ws_data.range("A2:B" & lrow) to ws_data.range("A3:B" & lrow), I receive an "Object required" error with that line.

Code:
            With ws_data
                'Set filter_rng = .Range("A3:B" & lrow)
                For Each rw In filter_rng.SpecialCells(xlCellTypeVisible)
                    t = rw.Row
                    inqseg = "[" & Format(ws_sdata.Cells(t, 5), "00") & "]"
                    flrow = Application.WorksheetFunction.Match("end", .Columns(8), 0) - 1
                    For fdest = 9 To flrow
                        fdest8 = .Cells(fdest, 8)
                        If fdest8 = inqseq Then
                            .Cells(fdest, 8).Interior.Color = ccgreen
                            .Cells(fdest, 6).Font.Color = ccgreen
                            .Cells(fdest, 6).Locked = False
                        End If
                    Next fdest
                Next rw
            End With
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Can't see enough code to provide focused answer so post what comes before?
Maybe it's that .Range isn't a member of ws_data?
 
Upvote 0
Hi Micron, thank your for your reply. I've attached the entire module in the hopes it helps. I had the same thought as you as far as whether the range was part of the object worksheet, so I tried to reset the range with:
VBA Code:
Set filter_rng = ws_data.Range("A3:B" & lrow)
recalling I received the same error.

Rich (BB code):
Sub btn_subaccomplishment_Click()
    'Stop
    Dim datestamp As Date
    Application.EnableEvents = False
    With ws_form
        datestamp = Now
        .Unprotect
        .Range("O3").Value = "'" & emplnum
        .Range("O4").Value = eqtnum
        .Range("E8") = "+"
        .Range("E8").Interior.ColorIndex = xlNone
        .Range("F8") = "[00]"
        .Range("G8") = ""
        .Range("H8") = "[00-00]"
        .Range("H8").Interior.ColorIndex = xlNone
        .Range("J8") = ""
        .Range("E4") = datestamp
        .Range("E4").NumberFormat = "dd-mmm-yy"
        .Range("E4").Value = Date
        .Range("E6") = Format(datestamp, "h:mm am/pm")
       
        zn = .Range("O5")
        If ws_segments.AutoFilterMode = True Then ws_segments.AutoFilterMode = False
        With ws_segments.Range("A1")
            .AutoFilter Field:=2, Criteria1:=zn
            .AutoFilter Field:=7, Criteria1:="PP"
            visiblepp = Application.WorksheetFunction.Sum(ws_segments.Columns(8).SpecialCells(xlCellTypeVisible))
            .AutoFilter Field:=2, Criteria1:=zn
            .AutoFilter Field:=7, Criteria1:="CL"
            visiblecl = Application.Application.WorksheetFunction.Sum(ws_segments.Columns(8).SpecialCells(xlCellTypeVisible))
            .AutoFilter Field:=2, Criteria1:=zn
            '.AutoFilter field:=2, Criteria1:="CW", Operator:=xlOr, Criteria2:="MUT", Operator:=xlOr, Criteria3:="PX"
            .AutoFilter Field:=7, Criteria1:=Array("CW", "MUT", "PX"), Operator:=xlFilterValues
            visiblecw = Application.WorksheetFunction.Sum(ws_segments.Columns(8).SpecialCells(xlCellTypeVisible))
        End With
        If ws_segments.AutoFilterMode = True Then ws_segments.AutoFilterMode = False
        .Range("S4") = visiblecw
        .Range("S5") = 0
        .Range("U4") = visiblepp
        .Range("U5") = 0
        .Range("W4") = visiblecl
        .Range("W5") = 0
       
        clear_segments
       
        process_segments '{Process}
        rwend = Application.WorksheetFunction.Match("end", .Columns(8), 0)
        process_surfaceconditions
        process_operations
       
        'hide operations/surface
        .Unprotect
        .Rows(rwend + 1 & ":" & rwend + 16).Hidden = True
        .Shapes("btn_sendhome").Visible = False
        .Activate
        RemoveCellSelectionBox
        ActiveWindow.ScrollRow = 1 'the row you want to scroll to
        ActiveWindow.ScrollColumn = 1
        'Stop
' ------------------------------
        'Check if data already exists for ref
        'create ref to filter (date+employee number+zone+corridor+segment)
        Stop
        inqdate = Format(.Range("E4"), "00000")
        inqempl = Format(.Range("O3"), "00000")
        inqzn = .Range("O5")
        inqcorr = Mid(.Range("F8"), 2, 2)
        inqtofind = inqdate & inqempl & inqzn & inqcorr
        'Filter Data
        With ws_sdata
            If .AutoFilterMode = True Then .AutoFilterMode = False
            lrow = .Cells(.Rows.Count, "B").End(xlUp).Row
            Set filter_rng = .Range("A2:B" & lrow)
            filter_rng.AutoFilter Field:=2, Criteria1:=inqtofind & "*"
            For y = 3 To .Range("B2").CurrentRegion.Rows.Count
                If .Cells(y, 2).EntireRow.Hidden = False Then
                    zcount = zcount + 1
                End If
            Next y
        End With
        Stop
        If zcount = 0 Then 'filter has results
            .Protect
            Application.EnableEvents = True
            Exit Sub
        Else
            .Rows("9:" & rwend).Hidden = False
            With ws_data
                'Set filter_rng = .Range("A3:B" & lrow)
                For Each rw In filter_rng.SpecialCells(xlCellTypeVisible)
                    t = rw.Row
                    inqseg = "[" & Format(ws_sdata.Cells(t, 5), "00") & "]"
                    flrow = Application.WorksheetFunction.Match("end", .Columns(8), 0) - 1
                    For fdest = 9 To flrow
                        fdest8 = .Cells(fdest, 8)
                        If fdest8 = inqseq Then
                            .Cells(fdest, 8).Interior.Color = ccgreen
                            .Cells(fdest, 6).Font.Color = ccgreen
                            .Cells(fdest, 6).Locked = False
                        End If
                    Next fdest
                Next rw
            End With
        End If

        'loop through filtered range and filling in selecting segment cells, unlocking colourized op cellc
        .Protect
    End With
    Application.EnableEvents = True
   
End Sub
 
Upvote 0
Where have you declared your object variables? I don't see ws_form or the others declared anywhere in that code. Are you using Option Explicit in your code?
 
Upvote 0
They are being publicly declared in another initialization type module. The others not so much.
 
Upvote 0
Micron, you sparked a thought that allowed me to find my error. I was setting my range to the wrong worksheet. All good now.
Thank you for all your effort, it wasn't something I feel you would have found. But you put me on the right track.
 
Upvote 0

Forum statistics

Threads
1,223,962
Messages
6,175,654
Members
452,664
Latest member
alpserbetli

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top