1

我试图仅将 ListObject 表的某些列的可见单元格放入范围对象中。

这似乎不起作用。

dim rng as range
with activesheet.listobjects("Tab_data").databodyrange
    set rng=.specialcells(xlcelltypevisible)
end with

但是当我选择整个范围然后偏移第一列以选择其他 2 个所需的列时,这有效!

dim rng as range
with activesheet.usedrange
    Set rng = .Offset(1, 1).Resize(.Rows.Count-1, .Columns.Count-1).SpecialCells(xlCellTypeVisible)
end with

但我不能在公式中使用上述内容,因为我的公式仅引用了 listobject 中的 2 列,如下所示:

在此处输入图像描述

工作表上的 UDF 公式:

=TagCloud(RngWrdLst as Range)

我将其用作:

=TagCloud(tab_data[[Brands]:[Index]])

正如您从图像中看到的那样,我只想要“品牌”和“索引”列中的可见单元格范围,而不是“COLUMN”列中的单元格。

所以我想要的可见范围是:

"$B$2:$C$3,$B$45:$C$45,$B$75:$C$78"

编辑@Jeeped:

如果我有一个从工作表单元格调用的 UDF 函数并传递了列 B 和 C 的 ListObject 范围(仅这些列而不是整个 databodyrange),那么我将如何找到 RngWrdLst 可见范围?

例如

从工作表调用:

=TagCloud(tab_data[[Brands]:[Index]])

函数定义:

Function TagCloud(RngWrdLst As Range)
Dim VisibleRng As Range

With RngWrdLst
    Set VisibleRng = Intersect(.SpecialCells(xlCellTypeVisible), Union(.Columns(2), .Columns(3)))
    Debug.Print VisibleRng.Address(0, 0)
End With

'   do something with the visibleRng......
End Function

顺便说一句,RngWrdLst 将包含 2 列 B 和 C。那么我如何修改您的代码并仅从函数中获取可见范围?

4

1 回答 1

1

对所需列的Union 方法使用Intersect 方法。

Dim rng As Range
With ActiveSheet.ListObjects("Tab_data").DataBodyRange
    Set rng = Intersect(.SpecialCells(xlCellTypeVisible), _
                        Union(.Columns(2), .Columns(3)))
    Debug.Print rng.Address(0, 0)
End With

或者,从第一列右移并调整比.DataBodyRange 属性包含的小一列的大小。

Dim rng As Range
With ActiveSheet.ListObjects("Tab_data").DataBodyRange
    With .Resize(.Rows.Count, .Columns.Count - 1).Offset(0, 1)
        Set rng = .SpecialCells(xlCellTypeVisible)
    End With
    Debug.Print rng.Address(0, 0)
End With

根据您想要处理的内容rng,您可能需要遍历Range.Areas 属性

于 2016-03-23T23:48:40.440 回答