VBA过滤结果行计数错误

时间:2014-10-21 16:37:49

标签: excel vba excel-vba

语言:Excel VBA

方案: 我有一个源范围(rngDTRef_AllRecord),我需要将数据插入目标范围(rngRTDC_AllDetail)

对于源范围(rngDTRef_AllRecord)中的每一行(rngCurrRow),它将过滤目标范围(rngRTDC_AllDetail)

如果过滤器产生结果,它会将一些数据添加到结果行(注意:每个结果都是唯一的)

否则它会在目标范围内添加一个新行(rngRTDC_AllDetail)

下面是代码:

    For Each rngCurrRow In rngDTRef_AllRecord.Rows
        intRTDC_RowBegin = 7
        intRTDC_ColIdxTotal = 20
        intRTDC_RowLast = fntGetNewLastRow 'this is some function get last row of rngRTDC_AllDetail due to might add in new row
        Set rngRTDC_AllDetail = shtRTDC.Range(shtRTDC.Cells(intRTDC_RowBegin, 1), shtRTDC.Cells(intRTDC_RowLast, intRTDC_ColIdxTotal))

        rngRTDC_AllDetail.AutoFilter
        rngRTDC_AllDetail.AutoFilter Field:=intRTDC_ColIdxAcc, Criteria1:=rngCurrRow.Cells(1, intDTSource_ColIdxAccCode), Operator:=xlAnd
        rngRTDC_AllDetail.AutoFilter Field:=intRTDC_ColIdxText, Criteria1:=rngCurrRow.Cells(1, strCurrAccCodeText), Operator:=xlAnd

        Dim rngResult As Range
        Set rngResult = rngRTDC_AllDetail.rows.SpecialCells(xlCellTypeVisible)'rngRTDC_AllDetail.SpecialCells(xlCellTypeVisible) also not work

        'after filter, it will be only 1 result or none
        If (rngResult.Rows.Count > 0) Then 
            'if the filter have result, do something here.
        else
            'add new row
        End If
    Next

我的问题是在过滤器之后,从excelworksheet,我可以看到只有1条记录,但是 rngResult.Rows.Count = 2'为rngRTDC_AllDetail中的第一个过滤器记录(只有1行),我怀疑由于它包含标题,但我不确定是什么错误。

rngResult.Rows.Count = 1'表示剩余的1行

更糟糕的是当过滤器后没有记录时,rngResult.Rows.Count = 1

任何建议都将受到赞赏。 TQ

1 个答案:

答案 0 :(得分:1)

确定。花了一些时间后,我发现了解决方案。     以下是面对类似问题的人的一些注意事项。

目标: 插入"值"到columnC,何时 columnA =" a" AND columnB =" b"并且该行仅在1到10之间

    A               B               C
1   columnA     |   columnB     |   ColumnC
2   a           |   b           |   value
3   a           |   x           |   
4   x           |   x           |
5   x           |   x           |
6   c           |   b           |
7   a           |   b           |   value
8   x           |   x           |
9   x           |   x           |
10  a           |   b           |   value
11  a           |   b           |   
12  a           |   b           |   
...

'insert value at columnC
ActiveSheet.Range("A1:B10").AutoFilter Field:=1, Criteria1:="a", Operator:=xlAnd
ActiveSheet.Range("A1:B10").AutoFilter Field:=2, Criteria1:="b", Operator:=xlAnd

Dim rng As Range
For Each rng In ActiveSheet.AutoFilter.Range.Range("A1:B10").SpecialCells(xlCellTypeVisible).Rows
    If (rng.Row <> 1) Then 'no the header
        ActiveSheet.Cells(rng.Row, "c") = "value" 'set value at C2,C7,C10
    End If
Next rng

'count the total row visible 
Dim rngA As Range
Set rngA = ActiveSheet.AutoFilter.Range.Range("A1:B10")
Debug.Print rngA.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1   'result 3
'Reference:http://www.contextures.com/xlautofilter03.html

Note1 **:&#34; ActiveSheet.AutoFilter.Range&#34;将始终包含标题和所有下面的行作为可见行。

Note2 **:&#34; ActiveSheet.AutoFilter.Range.Offset(1,0).SpecialCells(xlCellTypeVisible).Rows&#34;将仅偏移下面1行的范围,如果需要在结果行设置值,则不适合。