Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

filter regions graphs

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Filter regions of graphs

Filter regions of graphs

ResolvedVersion 2010

Laura has attended:
Excel VBA Introduction course
Excel VBA Intermediate course

Filter regions of graphs

Hi,
I've got a scatter graph that I've splitted into a grid of 9 boxes (grid lines made through additional data series). Boxes are labelled 4,2,1 on the top line, 7,5,3 or the middle line and 9,8,6 on the bottom line (because top right corner has highest priority therefore is called 1 and bottom left has lowest priority therefore is 9).

The grid position is set so that the lines always fall at the 33rd and 67th percentiles of the data sets. I want the user to be able to filter the graph by clicking on a button/clicking on checkboxes so that they can view points in selected boxes only eg only points in boxes 1 and 5 are visible. I've attempted to do this a macro linked to a button. The macro code is dependent on checkboxes linked to cells that show either true or false.

So far I've only the code to filter the x axis field, but am hitting some problems, I think through my use of "and" and "or". Code is shown below.


Sub FilterByBox()

If Range("box_1").Value = True Or Range("box_3").Value = True Or Range("box_6").Value = True And _
Range("box_2").Value = False And Range("box_5").Value = False And Range("box_8").Value = False And _
Range("box_4").Value = False And Range("box_7").Value = False And Range("box_9").Value = False Then


Range("prioritise").AutoFilter Field:=10, Criteria1:=">=" & Range("U13").Value


ElseIf Range("box_1").Value = True Or Range("box_3").Value = True Or Range("box_6").Value = True And _
Range("box_2").Value = True Or Range("box_5").Value = True Or Range("box_8").Value = True And _
Range("box_4").Value = False And Range("box_7").Value = False And Range("box_9").Value = False Then


Range("prioritise").AutoFilter Field:=10, Criteria1:=">=" & Range("T13").Value

ElseIf Range("box_1").Value = True Or Range("box_3").Value = True Or Range("box_6").Value = True And _
Range("box_4").Value = True Or Range("box_7").Value = True Or Range("box_9").Value = True And _
Range("box_2").Value = False And Range("box_5").Value = False And Range("box_8").Value = False Then

Range("prioritise").AutoFilter Field:=10, Criteria1:="<=" & Range("T13").Value, Criteria2:=">=" & Range("U13").Value

ElseIf Range("box_2").Value = True Or Range("box_5").Value = True Or Range("box_8").Value = True And _
Range("box_1").Value = False And Range("box_3").Value = False And Range("box_6").Value = False And _
Range("box_4").Value = False And Range("box_7").Value = False And Range("box_9").Value = False Then


Range("prioritise").AutoFilter Field:=10, Criteria1:=">=" & Range("T13").Value, Criteria2:="<=" & Range("U13").Value

ElseIf Range("box_2").Value = True Or Range("box_5").Value = True Or Range("box_8").Value = True And _
Range("box_4").Value = True Or Range("box_7").Value = True Or Range("box_9").Value = True And _
Range("box_1").Value = False And Range("box_3").Value = False And Range("box_6").Value = False Then

Range("prioritise").AutoFilter Field:=10, Criteria1:="<=" & Range("U13").Value

ElseIf Range("box_4").Value = True Or Range("box_7").Value = True Or Range("box_9").Value = True And _
Range("box_1").Value = False And Range("box_3").Value = False And Range("box_6").Value = False And _
Range("box_2").Value = False And Range("box_5").Value = False And Range("box_8").Value = False Then



Else
ActiveSheet.ListObjects("prioritise").Range.AutoFilter Field:=10


End If

End Sub


When box 1 and box 2 are checked I want it to go to the first ElseIf, but is seems to just be reading the initial If line of code instead.

Hope this makes sense...Do you have any suggestions on how to improve the code or a different approach I could use?

Thanks!

RE: Filter regions of graphs

Hi Laura,

Thanks for the forum question.

Normally when we are using a combination of and and or we need to group them using brackets:

Try :

If (Range("box_1").Value = True Or Range("box_3").Value = True Or Range("box_6").Value = True) And _
Range("box_2").Value = False And Range("box_5").Value = False And Range("box_8").Value = False And _
Range("box_4").Value = False And Range("box_7").Value = False And Range("box_9").Value = False Then


I hope this will solve your issue.




Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

Freeze Rows and Columns to keep lables displayed

You can freeze rows and columns in your worksheet so they don't move.

This allows you to keep row and column labels displayed on your screen as you move through a large worksheet.

Click below and/or to the right of the cell(s) you want to freeze. (NB. Excel freezes ALL the rows above and ALL the columns to left of the selected cell)

Click on the 'Windows' menu and selct 'Freeze Panes'.

Lines appear in your worksheet. The required rows and columns are frozen and remain on your screen as you move through your worksheet.

To unfreeze rows and columns, click on 'Window' menu and select 'Unfreeze Panes'.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.14 secs.