code not working - HELP !!

Kevin0427

Board Regular
Joined
Mar 31, 2016
Messages
69
Sub removeFilters()
ActiveWorkbook.RefreshAll

x = 1
ThisWorkbook.Sheets("Tracking Log").Unprotect Password:="test"
For x = 1 To 19
ThisWorkbook.Sheets("Tracking Log").Range("B14:T14").AutoFilter field:=x, visibledropdown:=False
Next x
ThisWorkbook.Sheets("Tracking Log").Range("B14:T14").AutoFilter field:=7, Criteria1:=1
ThisWorkbook.Sheets("Tracking Log").Protect Password:="test"
End Sub

The above code does not seem to work. This sub is called when the workbook is opened. The part that does not work is the refreshall. The filters all work. If I refresh manually it works but when I open the workbook it does not refresh the connections. ?!!?!?
 
Last edited:
Could be that on workbook_open, there's too much going on, and it's taking time to refresh the links. You could try:
Code:
Private Sub Workbook_Open()
DoEvents
        Call removeFilters
End Sub

Hi there. Possibly the refresh is still running, and then when you re-protect it at the end of the code thats when the error occurs? If so, try inserting this line after the refreshall line:
Code:
application.CalculateUntilAsyncQueriesDone

Thanks to both of you. I added both of these and the error is gone but now I get a 1004 error on the filter part that I did not get before. The DoEvents code did not seem to make a difference. And the data is not updating.
 
Last edited:
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
If it is the fact that the workbook just needs more time to finish getting things ready, you could put in a wait command:
Code:
Private Sub Workbook_Open()
Application.Wait (Now + TimeValue("0:00:10"))
        Call removeFilters
End Sub
... will delay the removeFilters from being called, by 10 seconds. You could then experiment with more/less delay until it works...
 
Upvote 0
If it is the fact that the workbook just needs more time to finish getting things ready, you could put in a wait command:
Code:
Private Sub Workbook_Open()
Application.Wait (Now + TimeValue("0:00:10"))
        Call removeFilters
End Sub
... will delay the removeFilters from being called, by 10 seconds. You could then experiment with more/less delay until it works...

The error comes in the removeFilters sub. I will try adding the wait in there instead.
 
Upvote 0
...but you've already proved that if the workbook's open for long enough,the code runs without a hitch, so I'd suggest putting the "Wait" in BEFORE calling the filter code.
Either way, let us know...
 
Upvote 0
I am getting a 1004 error in this loop in my removeFilters sub.

Code:
For x = 1 To 19
    ThisWorkbook.Sheets("Tracking Log").Range("B14:T14").AutoFilter field:=x, visibledropdown:=False
Next x
 
Upvote 0
...but you've already proved that if the workbook's open for long enough,the code runs without a hitch, so I'd suggest putting the "Wait" in BEFORE calling the filter code.
Either way, let us know...

I tried both. Neither work. The code never runs when the book is opened. Only if I do it manually or step through the code manually.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

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