Loop through an Excel sheet with an Autofilter on and copy ONLY specific column values from these filtered rows

52 views Asked by At

I am applying a filter to a sheet.

' Apply filter to column FORMATTED_INC_DATE_MONTH_YEAR and PRIORITY3
sourceRange.Range("DB1").AutoFilter field:=106, Criteria1:=strEOM_Selected
sourceRange.Range("DE1").AutoFilter field:=109, Criteria1:="Critical", Operator:=xlOr, Criteria2:="High"

I need to loop through each filtered row only, and retrieve a number of values from a predefined list of columns:

' Loop through the filtered rows and only copy the relevant column's values to source sheet
Set rngFiltered_Range = sourceRange.Range("DB1").SpecialCells(xlCellTypeVisible).EntireRow
    Debug.Print rngFiltered_Range.Address
    For Each rng In rngFiltered_Range
        If rng.Row <> 1 Then   'Ignore header row   
            Debug.Print rng.Address            
            strPRIORITY3 = rng.Cells(rng.Row, 109)                                                                                      
            strINC_DATE = rng.Cells(rng.Row, 4)                                                                                        
            strINC_NUM = rng.Cells(rng.Row, 1)                                                                                          
            strINC_DESC = rng.Cells(rng.Row, 3)                                                                                         
            strPIR_Problem_Owner = FindCellValueByIncident(sourceSheet2, "PIR_Problem_Owner", strINC_NUM)                              
            strOR_CR_Root_Cause_Classification = FindCellValueByIncident(sourceSheet2, "OR_CR_Root_Cause_Classification", strINC_NUM)  
            strRoot_Cause_Owner = rng.Cells(rng.Row, 129)                                                                               
            strImpacted_Division = rng.Cells(rng.Row, 2)                                                                                
            strBusiness_Impact = rng.Cells() = FindCellValueByIncident(sourceSheet2, "App_Service_Impacts", strINC_NUM)  
            strIncident_Summary = rng.Cells() = FindCellValueByIncident(sourceSheet2, "MANCO_Summary", strINC_NUM)      
        End If
    Next

The issue is that rngFiltered_Range returns a grouped range address which can be difficult to extract the row number e.g. for $5:$7 I have no way of getting row 6. In addition $2585:$1048576 needs to be ignored as this is the empty white space after the filtered rows.

`rngFiltered_Range =` $1:$1,$5:$7,$13:$16,$28:$28,$39:$39,$41:$41,$44:$44,$48:$48,$51:$51,$53:$53,$56:$56,$59:$59,$2585:$1048576

Is there a method or procedure which just gives me the row numbers of the filtered rows so I can assign row values variables given the predefined columns?

1

There are 1 answers

0
Peter Lucas On

Resolved by using the .rows property

For Each rng In rngFiltered_Range.Rows
    If rng.Row <> 1 Then
        Debug.Print "Row: " & rng.Address
        If rng.Row <= lngLastRow Then
        .
        .