在启用自动筛选的情况下循环遍历 Excel 工作表,并从这些筛选的行中仅复制特定列值

问题描述 投票:0回答:1

我正在对一张纸应用过滤器。

' 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"

我只需要循环遍历每个过滤行,并从预定义的列列表中检索多个值:

' 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

问题是

rngFiltered_Range
返回分组范围地址,该地址可能很难提取行号,例如对于 $5:$7 我无法获取第 6 行。另外 $2585:$1048576 需要被忽略,因为这是过滤后的行之后的空白区域。

`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

是否有一种方法或过程只为我提供过滤行的行号,以便我可以在给定预定义列的情况下分配行值变量?

excel vba autofilter
1个回答
0
投票

使用 .rows 属性解决

For Each rng In rngFiltered_Range.Rows
    If rng.Row <> 1 Then
        Debug.Print "Row: " & rng.Address
        If rng.Row <= lngLastRow Then
        .
        .
© www.soinside.com 2019 - 2024. All rights reserved.