找到下面的第一个可见单元格并获得参考

时间:2014-04-15 11:04:57

标签: excel excel-vba visible autofilter vba

我正在尝试在使用自动过滤器时在标题后选择第一个可见单元格。

If First.AutoFilter.Range.Columns(1).SpecialCells(xlVisible).Count - 1 > 0 Then
Range("A1").Select
Do
ActiveCell.Offset(1, 0).Select
Loop While ActiveCell.EntireRow.Hidden = True

但是在下一步我需要使用这个单元格,所以我需要它的行号和列号。我怎么能得到它? (不使用lastrow和lastcolumn)

4 个答案:

答案 0 :(得分:2)

你可以尝试这个(前三行用作示例代码)

已更新用户评论

Dim rng1 As Range
Dim rng2 As Range
Set rng1 = [a1:a10]
rng1.AutoFilter Field:=1, Criteria1:="4"
If rng1.SpecialCells(xlVisible).Count > 1 Then
    Set rng2 = rng1.Offset(1, 0).Resize(rng1.Rows.Count - 1, 1).SpecialCells(xlCellTypeVisible).Cells(1, 1)
    MsgBox rng2.Address
End If

答案 1 :(得分:0)

您可以使用ActiveCell.RowActiveCell.Column

答案 2 :(得分:0)

这也将循环到A列:

Option Explicit
Sub LoopThroughVisibleColA()

Dim MySheet As Worksheet
Dim FilterRange As Range, ColumnARange As Range, _
    Cell As Range
Dim LastRow As Long, LastCol As Long

'set worksheet for easy reference
Set MySheet = ThisWorkbook.Worksheets("Sheet1")

'identify the ranges for our filter and loop action
LastRow = MySheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastCol = MySheet.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Set FilterRange = Range(MySheet.Cells(1, 1), MySheet.Cells(LastRow, LastCol))
Set ColumnARange = Range(MySheet.Cells(1, 1), MySheet.Cells(LastRow, 1))

'apply filter: in this example, filter column A for values >4
FilterRange.AutoFilter Field:=1, Criteria1:=">4"

'loop through the visible cells in our column A range
For Each Cell In ColumnARange.SpecialCells(xlCellTypeVisible)
    If Cell.Row <> 1 Then 'skip the header row
        MsgBox ("We're in row " & Cell.Row & " and column " & Cell.Column)
    End If
Next Cell

End Sub

答案 3 :(得分:0)

您可以使用Application.Sendkeys "{DOWN}", True,然后阅读Selection.Address