我有一个范围变量“rng”。 我需要将rng设置为以下的交集: (1)表单上的usedrange排除第一列AND (2)第6列,例如
目前,我有:
Set rng = Intersect(.UsedRange, .Columns(6)).SpecialCells(xlCellTypeVisible)
' Because the range is filtered and i only need to select visible cells
但是这会返回一个也包含标题行的列。我只需要列中的数字。
(1)这样做的任何快速功能/方法/属性? (2)我如何找到这个范围的大小?即使rng中有多个单元格,rng.rows.count也会返回ONE。我应该使用rng.count吗?有什么区别?
非常感谢,
的Al
答案 0 :(得分:2)
我看到你已经接受了答案,但我看不出它如何回应你的要求它不包括标题行。这是我的解决方案。它还回答了关于如何获取行数的问题2:
Sub GetRangeAndCountRows()
Dim rng As Excel.Range
Dim rngArea As Excel.Range
Dim RowCount As Long
With ActiveSheet
Set rng = Intersect(.UsedRange.Resize(.UsedRange.Rows.Count - 1, .UsedRange.Columns.Count).Offset(1, 0), .Columns(6)).SpecialCells(xlCellTypeVisible)
Debug.Print rng.Address
For Each rngArea In rng.Areas
RowCount = RowCount + rngArea.Rows.Count
Next rngArea
Debug.Print RowCount
End With
End Sub
答案 1 :(得分:1)
蒂姆上面的评论当然很有效。
这个答案是为了与上面的代码保持一致,也可能更容易阅读。
添加 .UsedRange.Offset(,1)而不是 .UsedRange ,以忽略Interesect公式中UsedRange中的第一列。:
Set rng = Intersect(.UsedRange.Offset(,1), .Columns(6)).SpecialCells(xlCellTypeVisible)