为什么此过滤器适用于与我指定的范围不同的范围?

时间:2019-09-12 20:32:12

标签: excel vba

我想过滤特定的一行,但是出于某种原因,我的代码正在过滤前面的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

1 个答案:

答案 0 :(得分:0)

我必须在要过滤的行上方添加一个空行,以使AutoFilter可以获取确切的行。这尤其与多索引表有关。

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