简体   繁体   English

在清除VBA中另一个工作表中的过滤器后,将数据复制并粘贴到新工作表中

[英]Copy and paste data in a new sheet after clearing the filter in another sheet in VBA

The new sheet did not display the whole data after I click "All" button once to display all the data in new sheet after clearing the filter. 单击过滤器后,单击一次 “全部”按钮以显示新工作表中的所有数据后,新工作表显示全部数据。 However, it only shows the whole data when I click "All" button twice . 但是,当我两次单击“全部”按钮时,它仅显示整个数据。

Here is my code: 这是我的代码:

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

You might try this code and tell me if your issue is solved, i think that select was the issue. 您可以尝试使用此代码,并告诉我您的问题是否解决,我认为select就是问题。

Also please note that Thisworkbook appoint the workbook where the macro is coded 同时请注意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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM