Add a rule to the VBA to remove filters

Ramadan

New Member
Joined
Jan 20, 2024
Messages
16
Office Version
  1. 2021
Platform
  1. Windows
I have a code to copy data bewteen two different workbooks while the source file is closed and it's working perfectly but with one problem which is when the source file is closed with active filters in the table i get a problem in the data copied to the destination file

So, what I need is to add a rule to the code to clear all filters in the soruce file "stop work" before copying the data so that all data can be transfered to the destination file

and here is my code

VBA Code:
[Sub Update()

  ' Open the source workbook
Set sourceWorkbook = Workbooks.Open(Filename:="D:\Desktop\Stop Work.xlsm")

sourceWorkbook.Worksheets("Sheet1").Cells.Copy

' Open the destination workbook
Set destinationWorkbook = Workbooks.Open(Filename:="D:\Desktop\AUTHs.xlsm")
 
destinationWorkbook.Worksheets("Stop Work").Cells.PasteSpecial xlPasteAll

destinationWorkbook.Save

' Close the source workbook after saving changes
sourceWorkbook.Close SaveChanges:=False

 ActiveSheet.ListObjects(1).ListColumns(1).Range.End(xlDown).Select
  
   
End Sub]
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Reminder: Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Add a rule to the VBA to remove filters
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Reminder: Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Add a rule to the VBA to remove filters
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
ok understood - thanks for clarification
 
Upvote 0
You've not specified if there is just autofilter or excel tables which are filtered.
So let's try to cover both cases


VBA Code:
Sub Update()

  ' Open the source workbook
Set sourceWorkbook = Workbooks.Open(Filename:="D:\Desktop\Stop Work.xlsm")

with sourceWorkbook.Worksheets("Sheet1")

  .AutoFilterMode = False
  Dim lo As Long
  For lo = 1 To .ListObjects.Count
    .ListObjects(lo).ShowAutoFilter = False
  Next lo

  .Cells.Copy

end with

' Open the destination workbook
Set destinationWorkbook = Workbooks.Open(Filename:="D:\Desktop\AUTHs.xlsm")
 
destinationWorkbook.Worksheets("Stop Work").Cells.PasteSpecial xlPasteAll

destinationWorkbook.Save

' Close the source workbook after saving changes
sourceWorkbook.Close SaveChanges:=False

 ActiveSheet.ListObjects(1).ListColumns(1).Range.End(xlDown).Select
  
   
End Sub
 
Upvote 0
Solution
You've not specified if there is just autofilter or excel tables which are filtered.
So let's try to cover both cases


VBA Code:
Sub Update()

  ' Open the source workbook
Set sourceWorkbook = Workbooks.Open(Filename:="D:\Desktop\Stop Work.xlsm")

with sourceWorkbook.Worksheets("Sheet1")

  .AutoFilterMode = False
  Dim lo As Long
  For lo = 1 To .ListObjects.Count
    .ListObjects(lo).ShowAutoFilter = False
  Next lo

  .Cells.Copy

end with

' Open the destination workbook
Set destinationWorkbook = Workbooks.Open(Filename:="D:\Desktop\AUTHs.xlsm")
 
destinationWorkbook.Worksheets("Stop Work").Cells.PasteSpecial xlPasteAll

destinationWorkbook.Save

' Close the source workbook after saving changes
sourceWorkbook.Close SaveChanges:=False

 ActiveSheet.ListObjects(1).ListColumns(1).Range.End(xlDown).Select
 
  
End Sub
thank you so mucg for your help - it works perfectly
 
Upvote 0

Forum statistics

Threads
1,225,197
Messages
6,183,499
Members
453,165
Latest member
kuldeep08126

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