Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 502
  • Last Modified:

Autorun autofilter upon opening a worksheet

Hi,  (I might have figured this out a long time ago, albeit slowly, but I was never very good at VBA) :(


I have the same advanced filter on a few worksheets (in same workbook) and I need the auto-filter to immediately auto-run when the worksheet is opened.  That is, when a  worksheet that has an advanced filter set in it.

Can you help please.  

BTW: If you want a copy of the workbook please yell & I'll have to dummy one up for you (as it's confidential data).
0
kazmdav
Asked:
kazmdav
  • 6
  • 4
1 Solution
 
gowflowCommented:
could you post the workbook so we make sure it is done correctly.
But offhand whatever code to prepare for autofilter if you want it to be activated when you open the workbook then you should make sure the code is in this Sub

Private Sub Workbook_Open()

End Sub

Open in new window


gowlfow
0
 
kazmdavAuthor Commented:
Sure, no worries! A dummied up sample workbook is attached.  Example use:  In 'Foundation Skills (1)' in Col A, user selects from drop down. Foundation Skills (2) has the advanced filter (ie; filter out the rows that had 'No' selected). It's this filter that I want to occur when this worksheet is opened.

Same concept for Specialisation (1) & Specialisation (2) worksheets.  Thus when Specialistation (2) is opened I want the advanced filter to occur too.


Hope this helps!
Sample-workbook.xls
0
 
gowflowCommented:
ok lets take it step by step.

First I see some errors in sheet Foundation Skills (2) Row 3, 4, 5, 6 is it safe to delete these rows or something else should go there ?? as they point to Foundation Skills (1) #REF! ref means that the reference to the mentioned sheet is missing.

gowflow
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
kazmdavAuthor Commented:
Yes no problems, safe to delete them (it's as a result of me removing heaps of confidential data from Foundation skills (1)).

cheers!
0
 
gowflowCommented:
Furthermore I noticed that maybe in your sample workbook you made a crucial mistake of design where you included the choices in the sheets (2) like if the user go on sheet 2 and select a No then it will disappear from that sheet and will still show no value in the initial sheet 1

I think your aim is to have the user select in sheet 1 and have the sheets 2 for lookup and have it filtered excluding No Am I right ???
gowflow
0
 
gowflowCommented:
Also what about the option of No but interested ? you want to be filtered out or kept showing ??
gowflow
0
 
gowflowCommented:
ok here it is

Having not answered my previous question,

I have assumed that you input in sheets labeled 1 and have sheets labeled 2 to filter data excluding the No items (it will still show the No but interested items. I have removed the Validation from sheets (2) to prevent any input in these sheets accidentally that will not be reflected in Sheets (1)

Your problem actually is not only in the Open of the workbook but actually all the time the workbook is in the works. As you posted an .xls version the behavior in Excel 2003 is that the filter need to be activated so see the items filtered.

The version I am posting will thru macro activate the filtering in all time from open to close based on showing all items except the ones that are strictly No. If you need to change this behavior then, the code will need to be changed.

I have put the same code in both events. I will post the code for your convenience. You will need to put the same code in your production workbook.

Workbook_SheetChange
Workbook_Open

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim WSFound2 As Worksheet
Dim WSSpeci2 As Worksheet

Set WSFound2 = Sheets("Foundation Skills (2)")
Set WSSpeci2 = Sheets("Specialisation (2)")

WSFound2.Range("A:A").AutoFilter Field:=1, Criteria1:="<>" & "No"
WSSpeci2.Range("A:A").AutoFilter Field:=1, Criteria1:="<>" & "No"
End Sub

Open in new window


Private Sub Workbook_Open()
Dim WSFound2 As Worksheet
Dim WSSpeci2 As Worksheet

Set WSFound2 = Sheets("Foundation Skills (2)")
Set WSSpeci2 = Sheets("Specialisation (2)")

WSFound2.Range("A:A").AutoFilter Field:=1, Criteria1:="<>" & "No"
WSSpeci2.Range("A:A").AutoFilter Field:=1, Criteria1:="<>" & "No"
End Sub

Open in new window


Pls check out the file and let me know your comments.
gowflow
Sample-workbook.xls
0
 
kazmdavAuthor Commented:
Perfect! Thank you gowflow. Exactly what I wanted & I don't believe I require anything further.

I will put the code into production workbook tomorrow.  After the update & successful test in prod, I will return here to close the question.  

Sorry for no response; was on way home from work.

Cheers!
0
 
gowflowCommented:
No problem take your time and pls feel free to post here any link to any question you may need help with.
Rgds/gowflow
0
 
kazmdavAuthor Commented:
All good. Thank you. :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now