简体   繁体   中英

Select first visible cell directly beneath the header of a filtered column

I am trying to select the first visible cell directly beneath the header of a filtered column. The code I am getting is as below, but I have to problems with this code. First, the first line of code is using the current active range of the file. It is highly likely that this file will change and this range will not be the same. How can I make it work for any file I would use it on? Second, if I use a totally different file with the same column format, the first visible cell under Column J could be J210. How can I make this work for any array of variables?

Sub Macro16()
'
' Macro16 Macro
'

'
    ActiveSheet.Range("$A$1:$R$58418").AutoFilter Field:=12, Criteria1:= _
        "Sheets"
    Range("J2").Select
    ActiveCell.FormulaR1C1 = "=RIGHT(RC[1],3)"
    Selection.FillDown
End Sub

Untested but:

Sub Macro16()

    With ActiveSheet.Range("A1").CurrentRegion
        .AutoFilter field:=12, Criteria1:="Sheets"
        If .Columns(1).SpecialCells(xlCellTypeVisible).count > 1 Then
            With .Columns(10)
                .Resize(.rows.count - 1).offset(1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=RIGHT(RC[1],3)"
            End With
        End If
    End With

End Sub

I prefer non-destructive methods of determining whether there are visible cells to work with after a filtering operation. Since you are filling in column J with a formula, there is no guarantee that column J contains any values tat can be counted with the worksheet's SUBTOTAL function (SUBTOTAL does not count rows hidden by a filter) but the formula you are planning to populate into column J references column K so there must be something there.

Sub Macro16()
    With ActiveSheet
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            .Columns(12).AutoFilter Field:=1, Criteria1:="Sheets"
            With .Resize(.Rows.Count - 1, 1).Offset(1, 9)
                If CBool(Application.Subtotal(103, .Offset(0, 1))) Then
                    .SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=RIGHT(RC[1],3)"
                End If
            End With
            .Columns(12).AutoFilter Field:=1
        End With
    End With
End Sub

将配方填充到可见细胞

Sub FirstVisibleCell()
    With Worksheets("You Sheet Name").AutoFilter.Range
       Range("A" & .Offset(1, 0).SpecialCells(xlCellTypeVisible)(1).Row).Select
    End With
End Sub

Something like this might work...

Sub Macro16()

    Dim ARow As Long, JRow As Long, ws1 As Worksheet
    ws1 = Sheets("NAME OF SHEET WITH DATA")
    ARow = ws1.Range("A" & ws1.Rows.Count).End(xlUp).Row + 1
    ws1.Range("$A$1:$R$" & ARow).AutoFilter Field:=12, Criteria1:="Sheets"
    JRow = ws1.Range("J" & ws1.Rows.Count).End(xlUp).Row + 1
    ws1.Range("J" & JRow).FormulaR1C1 = "=RIGHT(RC[1],3)"
    ws1.Range("J" & JRow).FillDown
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM