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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33376775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:03:33  来源:igfitidea点击:

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

excelvbaexcel-vba

提问by Todd

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?

我正在尝试选择过滤列标题正下方的第一个可见单元格。我得到的代码如下,但我对这段代码有问题。首先,第一行代码使用文件的当前活动范围。这个文件很可能会改变,这个范围也不会相同。我怎样才能使它适用于我将使用它的任何文件?其次,如果我使用具有相同列格式的完全不同的文件,则列 J 下的第一个可见单元格可能是 J210。我怎样才能使这适用于任何变量数组?

Sub Macro16()
'
' Macro16 Macro
'

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

采纳答案by Rory

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

回答by Orin Moyer

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

回答by Rory

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 列包含任何值 tat 可以使用工作表的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

??????Fill Formula to Visible cells

??????将公式填充到可见单元格

回答by Chrismas007

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:$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