我尝试了Loop through cells and add to a range的可接受答案,但做了些许改动,但从未附加我的范围Arr
。
当我尝试调试它时,它只是第一个范围。 Union
从未成功。为什么会这样?
源代码:
Public Function VisibleRows(InRange As Range) As Range
Dim R As Range
Dim Arr As Range
Dim RNdx As Integer
Dim Count As Integer
For RNdx = 1 To InRange.Rows.Count
Set R = InRange(RNdx)
If R.EntireRow.Hidden = False And R.Value2 <> "" Then
If Arr Is Nothing Then
Set Arr = R
Else
Set Arr = Union(Arr, R)
End If
End If
Next RNdx
VisibleRows = Arr
End Function
答案 0 :(得分:3)
我可以看到一些代码问题:
您正在逐行循环,但是表达式InRange(RNdx)
在范围内使用了RNdx的第 cell 条-它先水平然后垂直。您可能想要InRange.Cells(RNDx, 1)
应为Set VisibleRows = Arr
答案 1 :(得分:2)
您的函数正在返回范围对象。范围对象被分配给单词Set
的变量。您没有使用这个词。试试运行TestMe()
。
Option Explicit
Public Sub TestMe()
VisibleRows(Range("A1:A10")).Select
End Sub
Public Function VisibleRows(InRange As Range) As Range
Dim R As Range
Dim Arr As Range
Dim RNdx As Integer
Dim Count As Integer
For RNdx = 1 To InRange.Rows.Count
Set R = InRange(RNdx)
If R.EntireRow.Hidden = False And R.Value2 <> "" Then
If Arr Is Nothing Then
Set Arr = R
Else
Set Arr = Union(Arr, R)
End If
End If
Next RNdx
Set VisibleRows = Arr
End Function
这是它的示例结果: