简体   繁体   中英

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.

Also please note that Thisworkbook appoint the workbook where the macro is coded

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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