简体   繁体   English

选择筛选列标题正下方的第一个可见单元格

[英]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. 其次,如果我使用具有相同列格式的完全不同的文件,则列J下的第一个可见单元格可以是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. 由于您使用公式填充J列,因此无法保证列J包含可以使用工作表的SUBTOTAL函数计算的任何值(SUBTOTAL不计算过滤器隐藏的行)但是您计划填充的公式列J引用了列K,所以必须有一些东西。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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