98.7% Of all customers recommend us, we're so confident about our results we publish all reviews and stats
View Live Stats View ReviewsForum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Filter regions of graphs
Filter regions of graphs
Resolved · High Priority · Version 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 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
Any suggestions, questions or comments? Please post in the Improve the forum thread. |
Excel tip:Moving or Copying Sheets Between Workbooks in Excel 2010Here's how to move or copy sheets between workbooks in Excel 2010: |