VBA to a specific workbook.

Keibri

New Member
Joined
May 29, 2015
Messages
8
Hi all,

I have the following code which basically allows for advanced filtering to be done through VBA. If possible, I would like to run this macro to another workbook which is not yet active i.e. it is closed.

I think that what needs to be done is to adjust the code so as to open the specific workbook, run this code and then save the workbook and close it again.


Can someone guide me through!?

Thanks a lot!! :)


Code:
Option Explicit


Sub Filter_Copy()
   
   Dim LR           As Long
   Dim ws           As Worksheet
    
    Set ws = Sheets("Sheet2")


   With ws
      LR = .Range("D" & .Rows.Count).End(xlUp).Row
   End With
   Application.ScreenUpdating = False
   Application.DisplayAlerts = False
   On Error Resume Next
   Sheets("Lists").Delete
   On Error GoTo 0
   Application.DisplayAlerts = True
   
   
    Sheets("Archive").Range("A1:D600").ClearContents
  
   With ActiveSheet
      ws.Range("A4:D600" & LR).AdvancedFilter Action:=xlFilterCopy _
         , CriteriaRange:=Sheets("Sheet2").Range("A1:D2"), CopyToRange:=Sheets("Archive").Range("A1"), Unique:=True


   
   End With
   
   
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Modifications in RED:

Test this on a COPY of your File, NOT THE ORIGINAL..

Rich (BB code):
Sub Filter_Copy()
   
   Dim LR As Long
   Dim ws As Worksheet
   Dim wb As Workbook
   Dim sFile As String
   sFile = "C:\Users\Owner\Documents\MrExcel\MyTestFile.xlsx"  ' Change this to your Destination file path
   Workbooks.Open sFile
   Set wb = ActiveWorkbook
   
    Set ws = Sheets("Sheet2")

   With ws
      LR = .Range("D" & .Rows.Count).End(xlUp).Row
   End With
   Application.ScreenUpdating = False
   Application.DisplayAlerts = False
   On Error Resume Next
   Sheets("Lists").Delete
   On Error GoTo 0
   Application.DisplayAlerts = True
   
   
    Sheets("Archive").Range("A1:D600").ClearContents
  
   With ActiveSheet
      ws.Range("A4:D600" & LR).AdvancedFilter Action:=xlFilterCopy _
         , CriteriaRange:=Sheets("Sheet2").Range("A1:D2"), CopyToRange:=Sheets("Archive").Range("A1"), Unique:=True
    wb.Save
    wb.Close
   Application.ScreenUpdating = True
   End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,203,521
Messages
6,055,880
Members
444,830
Latest member
Excelsmallbusinessmom

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