首页 > 解决方案 > 在 VBA 中清除另一个工作表中的过滤器后,将数据复制并粘贴到新工作表中

问题描述

清除过滤器后,单击“全部”按钮一次以显示新工作表中的所有数据后,新工作表未显示全部数据。但是,当我单击“全部”按钮两次时,它只会显示整个数据。

这是我的代码:

Sub Reset1_button()

Application.ScreenUpdating = False
Dim LastRow As Long
Dim LRow As Long

LastRow = WorksheetFunction.Max(Sheets("SALES").Cells(Rows.Count, "A").End(xlUp).Row, 2)
LastRow = LastRow + 1
LRow = WorksheetFunction.Max(Sheets("ONE_ALLIANZ_REPORT").Cells(Rows.Count, "A").End(xlUp).Row, 18)
LRow = LRow + 1

Range("A19:AQ" & LRow).Clear
Sheets("SALES").Select

If (ActiveSheet.AutoFilterMode And ActiveSheet.FilterMode) Or ActiveSheet.FilterMode Then
  ActiveSheet.ShowAllData
End If

Sheets("SALES").Select
ActiveSheet.Range("A3:AQ" & LastRow).Copy
Sheets("ONE_ALLIANZ_REPORT").Select
ActiveSheet.Range("A19").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveCell.Select

End Sub

标签: excelvba

解决方案


您可以尝试使用此代码并告诉我您的问题是否已解决,我认为这select就是问题所在。

另请注意,Thisworkbook指定宏编码的工作簿

Sub Reset1_button()

Application.ScreenUpdating = False
Dim LastRow As Long
Dim LRow As Long

LastRow = WorksheetFunction.Max(ThisWorkbook.Sheets("SALES").Cells(Rows.Count, "A").End(xlUp).Row, 2)
LastRow = LastRow + 1
LRow = WorksheetFunction.Max(ThisWorkbook.Sheets("ONE_ALLIANZ_REPORT").Cells(Rows.Count, "A").End(xlUp).Row, 18)
LRow = LRow + 1

Range("A19:AQ" & LRow).Clear

If ThisWorkbook.Sheets("SALES").FilterMode Then
   ThisWorkbook.Sheets("SALES").ShowAllData
End If


 ThisWorkbook.Sheets("SALES").Range("A3:AQ" & LastRow).Copy Destination:=ThisWorkbook.Sheets("ONE_ALLIANZ_REPORT").Range("A19")

End Sub

推荐阅读