【问题标题】:Why does this filter apply on a different range than what I specify?为什么此过滤器适用于与我指定的范围不同的范围?
【发布时间】:2019-09-12 20:32:12
【问题描述】:

我想过滤特定的行,但无论出于何种原因,我的代码都会提前过滤 2 行。最奇怪的是,我对另一个工作表有完全相同的代码,该工作表在正确的行上进行过滤。

这里,行是 11,所以我要求它过滤 A11 范围。

工作簿的设置方式(合并单元格等)是否会导致这种情况?

Sub SplitByBoro(Boro As String)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'SplitByBoro
'This subroutine splits a worksheet by borough data. Enter your specific borough (i.e. "QFSN") and it will create a new
'tab labeled as your borough. It will then filter the data for your borough and paste only visible cells (i.e. filtered cells)
'as formatting and values.
'
'Parameters: Boro (String)
'Hard-coded Constants: Master Sheet (String), Master Table (String), Borough Column (Integer)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'Step 1:    Declare your variables
        Dim MasterFile As Workbook
        Dim newBoro As Worksheet
        Dim BoroughColumn As Integer
        Dim MasterSheet As String
        Dim MasterTable As String


'Step 2:    Hard-code master sheet name, table name, and borough column number to filter on. Check that values are accurate with each iteration.
        BoroughColumn = 1
        MasterSheet = "DETAIL - 3-8 ELA & MATH"
        MasterSheet2 = "DETAIL - 4, 8 SCIENCE"


'Step 3:    Select your workbook with all borough data
        Set MasterFile = ActiveWorkbook


'Step 4:    Create a new tab for your borough
        Set newBoro = MasterFile.Sheets.Add(Type:=xlWorksheet, After:=Application.ActiveSheet)
        newBoro.Name = Boro & " " & MasterSheet
        Set newBoro2 = MasterFile.Sheets.Add(Type:=xlWorksheet, After:=Application.ActiveSheet)
        newBoro2.Name = Boro & " " & MasterSheet2


'Step 5:    Copy over borough specific data using your filter
        With MasterFile
            'change summary boro
            .Sheets(MasterSheet).Cells.Range("D3").Value = Boro
            'filter for borough
            .Sheets(MasterSheet).Range("A11").AutoFilter field:=BoroughColumn, Criteria1:=Boro
            .Sheets(MasterSheet).Cells.SpecialCells(xlCellTypeVisible).Copy
            .Sheets(newBoro.Name).Paste
            'choose how to autofit
            .Sheets(newBoro.Name).Cells.Columns("A").AutoFit
            .Sheets(newBoro.Name).Cells.Columns("B").AutoFit

            'change summary boro
            .Sheets(MasterSheet2).Cells.Range("D3").Value = Boro
            'filter for borough
            .Sheets(MasterSheet2).Range("A11").AutoFilter field:=BoroughColumn, Criteria1:=Boro
            .Sheets(MasterSheet2).Cells.SpecialCells(xlCellTypeVisible).Copy
            .Sheets(newBoro2.Name).Paste
            .Sheets(newBoro2.Name).Cells.Range("A4").Value = Boro
            'choose how to autofit
            .Sheets(newBoro.Name).Cells.Columns("A").AutoFit
            .Sheets(newBoro.Name).Cells.Columns("B").AutoFit
        End With


End Sub

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    我必须在我尝试过滤的行上方添加一个空行,以便自动过滤器获取我的确切行。这尤其适用于多索引表。

    https://answers.microsoft.com/en-us/msoffice/forum/all/vba-code-applies-autofilter-to-the-wrong-row/fd7f584e-fd20-4844-bc0c-b55995100c61

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多