如何在第二行自动筛选

How to Autofilter on the second row

我的工作簿中有 3 张纸,我的代码检查 Sheet 2 中的活动单元格是否为空白,

If Len(ActiveCell.Value) = 0 Then MsgBox "Blank Key in:" & ActiveCell.Address, vbCritical Exit Sub End If 如果它不为空,那么我的代码会计算 Sheet 2 中的 Active Cell 在 Sheet 3 中出现的次数(如果有的话),如果大于 2 次,则会出现一个 Msgbox 框询问用户看到 "Previous Entries" 本质上自动过滤 Sheet 3 到 Sheet 2 的活动单元格值。这是我的整个代码:

Option Explicit
Sub Autofilter_Macro4()

 Application.ScreenUpdating = False

    Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet                       'Declares variables as worksheets
    Dim rng As Range                                                               'Declares variable as a range to store values

    Set sh1 = Sheet1                                                               'Assigns a worksheet to the declared worksheet variable (sh1 = "Main Database" Worksheet = Machine Inv #)
    Set sh2 = Sheet2                                                               'Assigns a worksheet to the declared worksheet variable (sh 2 = "Changes" Worksheet)
    Set sh3 = Sheet3                                                               'Assigns a worksheet to the declared worksheet variable (sh 3 = "Historical Parameters" Worksheet)

    Dim rowAC As Long, rowCut As Long                                              'Declares variable and assigns it as a Long data type

    rowAC = ActiveCell.Row                                                         'Sets the Long variable as the Active Cell Row in Sheet 2

    If Len(ActiveCell.Value) = 0 Then                                              'Tests if the Active Cell in column A (Key) of the "Changes" Worksheet is blank or not

        MsgBox "Blank Key in:" & ActiveCell.Address, vbCritical                    'If the Active Cell is blank, then this MsgBox notifies you that it's blank
        Exit Sub                                                                   'Ends the entire Macro if the Active Cell is Blank

    End If                                                                         'Doesn't initiate the MsgBox and continues the Macro if the Key in Column A is not blank
    Dim Source As Range

    Set Source = sh3.Range("A1", sh3.Range("A" & rows.Count).End(xlUp))                                 'Initializing "Source" variable range to last row in Sheet 3

    Dim Counter As Long
    Dim Result As String

    sh3.AutoFilterMode = False                                                                          'Clears any Autofilters (if any) in Sheet 3

    Counter = Application.WorksheetFunction.CountIf(Source, sh2.Range("A" & rowAC))                     'Counts # of times the ActiveCell is in the Source range

    If Counter > 2 Then                                                                                 'If there are more than 3 duplicates then display a message box

    Result = MsgBox("No. of Duplicates in the Historical Parameters Sheet is : " & Counter & vbNewLine & _
    "Do you want to see the Previous Key Entries?", vbYesNo + vbInformation, "Duplicate Key Entries")   'Msgbox displaying the number of duplicate values in Sheet 3

        If Result = vbYes Then

            MsgBox "Yes"

            sh3.Range("A:A").Autofilter Field:=1, Criteria1:=ActiveCell.Value                            'Autofilters Sheet 3 for the Active Cell (Key) from Sheet 2 ("Changes" Worksheet)

            sh3.Range("A2").Value = sh2.Range("MyRange").Value                                           'Sets the Value of Cell "A2" in Sheet 3 to the named range "MyRange" (ActiveCell) from Sheet 1

            sh3.Activate                                                                                 'Sets Sheet 3 as the active sheet

            ActiveWindow.FreezePanes = True                                                              'Attemps to Freezes the Panes (Top 2 rows) of Sheet 3

         Else:

            MsgBox "No"

         End If

    End If

Application.ScreenUpdating = True

End Sub

目前我正在尝试使用我的代码冻结 Sheet 3 中的前两个窗格,但第二行仍包含在自动过滤器中。

我想冻结 Sheet 3 中前两行的窗格,或者第二行的自动筛选器(因此不包括 Header 第 1 行和子 header 第 2 行)或另一个更简单的 code/solution。谢谢大家的帮助。

Cross-posted 在另一个平台上也是如此:https://www.mrexcel.com/board/threads/how-to-autofilter-on-the-second-row.1125449/

从第二行向下过滤....

Dim lr as Long
lr = sh3.Range("A" & sh3.Rows.Count).End(xlUp).Row

sh3.Range("A2:A" & lr).AutoFilter 1, ActiveCell.Value