设置从 VBA 中的 Copy() 方法返回的 Range 对象?

发布于 2024-12-11 20:15:33 字数 471 浏览 0 评论 0原文

我已经使用宏在 Excel 2003 中的筛选列表范围上使用了 Copy() 方法,并且我想获取返回的范围并计算行数(这样我可以检查我是否复制了不止一行。但是我得到一个 '需要对象”错误。Microsoft

网站上的 VBA 参考资料告诉我,Copy() 方法返回一个范围对象,但从代码来看,情况并非如此,请告知

For J = 1 To .ListObjects.Count
    ' Filter
    .ListObjects(J).Range.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=CritRange
    Set CopiedRange = .ListObjects(J).Range.Copy ' Object required error
    If CopiedRange.Rows.Count > 1 Then
    {etc...}

I have used the Copy() method on a filtered List range in Excel 2003 using a macro and I want to grab the returned range and count the rows (so I can check I've copied more than one row. However I get an 'Object required' error.

I am told by VBA reference on Microsoft's website that the Copy() method returns a range object. However from the code this appears to not be the case. Please advise!

Thanks.

For J = 1 To .ListObjects.Count
    ' Filter
    .ListObjects(J).Range.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=CritRange
    Set CopiedRange = .ListObjects(J).Range.Copy ' Object required error
    If CopiedRange.Rows.Count > 1 Then
    {etc...}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬色的秋千 2024-12-18 20:15:33

Excel 不会在 Copy() 操作中返回范围对象。我尝试在复制之前测试行数的范围,但是如果不考虑特别是仅针对可见行,这是行不通的。因此,我需要编写一个新函数:

Function CountVisibleRows(rg As Range)
    Dim NumRows As Integer
    ' Count visible cells in the first column, minus one to account for the header
    NumRows = rg.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1
    CountVisibleRows = NumRows
End Function

然后在复制之前在范围内测试它并确定要复制的行数(因为 Copy() 方法默认情况下仅复制可见行)。

Excel doesn't return a range object on a Copy() operation. I tried testing the range for the row count before copying it but this didn't work without taking into account especially for visible rows only. Hence I needed to write a new function:

Function CountVisibleRows(rg As Range)
    Dim NumRows As Integer
    ' Count visible cells in the first column, minus one to account for the header
    NumRows = rg.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1
    CountVisibleRows = NumRows
End Function

I then tested this on the range before copying and determined the number of rows that would be copied (because the Copy() method copies only visible rows by default).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文