AdvancedFilter Copy And Paste in Diferrent Workbooks ?

rodrigo_m_almeida

New Member
Joined
Jan 13, 2022
Messages
42
Office Version
  1. 2021
Platform
  1. Windows
Could anyone help ?

How can I do this...
Trying this way

Problem with advancedfilter !

VBA Code:
Public Sub SEARCH()

    ' Optimize
        Application.Visible = False: Application.ScreenUpdating = False: Application.DisplayAlerts = False
    ' Declare Variables
        Dim xPathName, xFileName As String, xOldWB, xNewWB As Workbook
    ' Browse Folder
        Set FileDialog = Application.FileDialog(msoFileDialogFolderPicker)
        FileDialog.Title = "Select Folder :"
        If FileDialog.Show = -1 Then xPathName = FileDialog.SelectedItems(1) Else Exit Sub
        If Right(xPathName, 1) <> "\" Then xPathName = xPathName + "\"
    ' Open File 02
        Workbooks.Add
        Set xNewWB = ActiveWorkbook
    ' Search File
        xFileName = Dir(xPathName & "FINAL*.xlsx")
    ' Open File 01
        Do While xFileName <> ""
 
            Workbooks.Open Filename:=xPathName & xFileName
            Set xOldWB = ActiveWorkbook
      
            ' Copy And Paste (Unique) for Other Workbook / >>> ERROR 1004 <<<
            xOldWB.Sheets(1).Columns("A:D").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=xNewWB.Sheets(1).Columns("A:D"), Unique:=True
      
    ' Save File 01

    ' Close File 01
            xOldWB.Close
    ' Next File
            xFileName = Dir
        Loop
    ' Save File 02
        xNewWB.SaveAs Filename:=xPathName & "ABC123", FileFormat:=xlWorkbookDefault
    ' Close File 02
        xNewWB.Close
    ' Optimize
        Application.Visible = True: Application.ScreenUpdating = True: Application.DisplayAlerts = True
 
Last edited:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi Rodrigo,
As a first comment I would advise you to use sub and function names that are NOT the same as inbuilt Excel function names (SEARCH()).

I will have to go through the code in depth to see what you are trying to achieve.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,828
Messages
6,181,210
Members
453,023
Latest member
alabaz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top