从ListObject表的特定列中选择SpecialCells(xlCellTypeVisible)

时间:2016-03-23 23:32:07

标签: excel excel-vba range visible listobject vba

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

这似乎不起作用。

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

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

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

但我不能在公式中使用上述内容,因为我的公式仅指下面列表对象中的2列:

enter image description here

工作表上的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函数并传递ListObject范围的列B& C(只有这些列,而不是整个databodyrange),那么我将如何找到RngWrdLst可见范围?

e.g。

从工作表调用:

=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
BTW,RngWrdLst将包含2列B& C.那么我如何修改代码并从函数中仅获得可见范围?

1 个答案:

答案 0 :(得分:1)

Intersect method所需的列上使用Union method

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 property所包含的列。

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 property