高级筛选不正确确定lastrow

这段代码按照我想要的方式工作,但是我不了解这个代码。 该代码合并来自多个表格的多个工作表的数据并粘贴其自己的工作表,然后应用高级filter。 问题是高级的filter代码不正确地确定数据的最后一行。

Worksheets("SummarySheet").Range("A18:G18" & Last).AdvancedFilter Action:=xlFilterInPlace, CriteriaRange _ :=Worksheets("SummarySheet").Range("F3:G17"), Unique:=False 

使用当前数据,最后一行数据在第55行。但是,当我运行代码认为数据的最后一行是在1835年。这里是代码:

 Option Explicit Sub CopyRangeFromMultiWorksheets2() Dim sh As Worksheet Dim DestSh As Worksheet Dim Last As Long Dim CopyRng As Range Dim tbl As ListObject Dim Cell As Range With Application .ScreenUpdating = False .EnableEvents = False End With On Error Resume Next ActiveWorkbook.Worksheets("SummarySheet").ShowAllData ' Delete the data off of summary sheet. Application.DisplayAlerts = False On Error Resume Next ActiveWorkbook.Worksheets("SummarySheet").Range("A19:G19" & Last).Cells.Clear On Error GoTo 0 Application.DisplayAlerts = True Set DestSh = ThisWorkbook.Worksheets("SummarySheet") On Error Resume Next ' Loop through all worksheets and copy the data to the ' summary worksheet. For Each sh In ThisWorkbook.Worksheets For Each tbl In sh.ListObjects For Each Cell In tbl.DataBodyRange.Rows If sh.Name <> DestSh.Name Then ' Find the last row with data on the summary worksheet. Last = LastRow(DestSh) ' Specify the range to place the data. Select entire row where cells are orange. Set CopyRng = Cell ' Test to see whether there are enough rows in the summary ' worksheet to copy all the data. If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then MsgBox "There are not enough rows in the " & _ "summary worksheet to place the data." GoTo ExitTheSub End If ' This statement copies values and formats from each ' worksheet. CopyRng.Copy With DestSh.Cells(Last + 1, "A") .PasteSpecial xlPasteValues .PasteSpecial xlPasteFormats Application.CutCopyMode = False End With End If Next Next Next Worksheets("SummarySheet").Range("A19:G19" & Last).Interior.ColorIndex = 0 Worksheets("SummarySheet").Range("A19:G19" & Last).Borders.LineStyle = Excel.XlLineStyle.xlLineStyleNone Worksheets("SummarySheet").Range("A18:G18" & Last).AdvancedFilter Action:=xlFilterInPlace, CriteriaRange _ :=Worksheets("SummarySheet").Range("F3:G17"), Unique:=False On Error Resume Next Worksheets("SummarySheet").Range("A3:A17").EntireRow.Hidden = True MsgBox "Done." ExitTheSub: Application.GoTo DestSh.Cells(1) ' AutoFit the column width in the summary sheet. DestSh.Columns.AutoFit With Application .ScreenUpdating = True .EnableEvents = True End With End Sub 

它看起来像错误是在这一行:

 Worksheets("SummarySheet").Range("A18:G18" & Last).AdvancedFilter _ Action:=xlFilterInPlace, CriteriaRange:=Worksheets("SummarySheet").Range("F3:G17"), Unique:=False 

如果最后是99,那么范围变成Range("A18:G1899)

 Worksheets("SummarySheet").Range("A18:G" & Last).AdvancedFilter _ Action:=xlFilterInPlace, CriteriaRange:=Worksheets("SummarySheet").Range("F3:G17"), Unique:=False 

但更好的是,

 with Worksheets("SummarySheet") .Range("A18:G" & Last).AdvancedFilter _ Action:=xlFilterInPlace, CriteriaRange:=.Range("F3:G17"), Unique:=False end with