简体   繁体   English

中断范围内的Rows.Count不正确(Excel VBA)

[英]Incorrect Rows.Count in an interrupted range (Excel VBA)

I'm trying to get the Rows.Count of a range which consists of two combined ranges: 我正在尝试获得包含两个组合范围的范围的Rows.Count:

Set rng = Union(Range1,Range2)

Unfortunately the rng.Rows.Count returns the Rows.Count of Range1, and I would expect it to return Rows.Count of Range1+Range2. 不幸的是,rng.Rows.Count返回Range1的Rows.Count,我希望它返回Range1 + Range2的Rows.Count。

The actual range I tested it with is this: $A$27:$G$41,$A$43:$G$43 我测试的实际范围是: $A$27:$G$41,$A$43:$G$43

Its Rows.Count property returns 15 but in this range there are 16 rows. 它的Rows.Count属性返回15但在此范围内有16行。

Any ideas how to get the correct Rows.Count without much juggling? 任何想法如何获得正确的Rows.Count没有太多的杂耍?

Try this 尝试这个

Dim arr as range
Dim rws as long

For each arr in rng.areas
    Rws = rws + arr.rows.count
Next

Try this: 尝试这个:

Sub Test()
    Dim rng As Range
    Res = 0
    Set rng1 = Range("A27:G41")
    Set rng2 = Range("A43:G43")
    Set rng = Union(rng1, rng2)
    For Each area In rng.Areas
        Res = Res + area.Rows.Count
        Debug.Print Res
    Next area
End Sub

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM