Home > Software engineering >  Unfilter a sheet and copy paste all the values in a range
Unfilter a sheet and copy paste all the values in a range

Time:08-10

I'm trying to write a code to solve the below use case:

I have 2 sheets, My need is to check that there are no filters on both sheets (in case yes they should be removed) and right after to clear all contents in the range of sheet 1 and copy paste the values from sheet 2

Here the code:

Sub AggiornaApp()

If Sheets("Applicazioni").AutoFilterMode Then
    Sheets("Applicazioni").AutoFilter.ShowAllData
End If

Sheets("Applicazioni").Range(Range("A9"), Range("B9").End(xlDown)).ClearContents 'i'm getting error 1004 here

If Sheets("Base Applicazioni").AutoFilterMode Then
    Sheets("Base Applicazioni").AutoFilter.ShowAllData
End If

  
Sheets("Base Applicazioni").Activate
Range(Range("A6"), Range("A6").End(xlDown)).Copy
Sheets("Applicazioni").Range("A9").PasteSpecial

Sheets("Base Applicazioni").Activate
Range(Range("B6"), Range("B6").End(xlDown)).Copy
Sheets("Applicazioni").Range("B9").PasteSpecial

End Sub

Unfortunately i'm getting run time error 1004 as indicated above, any idea?

CodePudding user response:

Regarding your error 1004: Sheet has to be activated before using the code "End(xlDown)" or any End(xlDown/UP/etc)

you can add before:

Sheets("Applicazioni").activate
Sheets("Applicazioni").Range(Range("A9"), Range("B9").End(xlDown)).ClearContents
  • Related