Please forgive the cross-posting - I know it's frowned upon but I'm in dire need as I'm up against a deadline of the end of this week to figure this out! Cross-post can be found here: http://www.excelforum.com/exce…-from-it.html#post4236408
I thought this was going to be an easy problem to solve, but I'm thinking that it's apparently far more complicated than I originally thought (sigh - isn't everything?)
My worksheet is divided up into 8 main sections: RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET, and FUCHSIA. I want the user to be able to hide and un-hide these sections at any time, so I've inserted a helper column (column N) to be able to filter as needed. I then included two macro buttons for each color section - button 1 would filter column N to remove the color from the displayed results (hide those colored rows) and button 2 would add the color back to the filter so that color would show back up in the displayed results (un-hide those colored rows).
[TABLE="class: grid, width: 500"]
[tr]
[td]
COLUMN N
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
RED SECTION HEADER ROW
[/td]
[td]
BUTTON 1 - HIDE RED SECTION
[/td]
[td]
BUTTON 2 - UN-HIDE RED SECTION
[/td]
[/tr]
[tr]
[td]
RED
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
RED
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
RED
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
ORANGE SECTION HEADER ROW
[/td]
[td]
BUTTON 1 - HIDE ORANGE SECTION
[/td]
[td]
BUTTON 2 - UN-HIDE ORANGE SECTION
[/td]
[/tr]
[tr]
[td]
ORANGE
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
ORANGE
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
ORANGE
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
YELLOW SECTION HEADER ROW
[/td]
[td]
BUTTON 1 - HIDE YELLOW SECTION
[/td]
[td]
BUTTON 2 - UN-HIDE YELLOW SECTION
[/td]
[/tr]
[tr]
[td]
YELLOW
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
YELLOW
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
YELLOW
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
GREEN SECTION HEADER ROW
[/td]
[td]
BUTTON 1 - HIDE GREEN SECTION
[/td]
[td]
BUTTON 2 - UN-HIDE GREEN SECTION
[/td]
[/tr]
[tr]
[td]
GREEN
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
GREEN
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
GREEN
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
BLUE SECTION HEADER ROW
[/td]
[td]
BUTTON 1 - HIDE BLUE SECTION
[/td]
[td]
BUTTON 2 - UN-HIDE BLUE SECTION
[/td]
[/tr]
[tr]
[td]
BLUE
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
BLUE
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
BLUE
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
INDIGO SECTION HEADER ROW
[/td]
[td]
BUTTON 1 - HIDE INDIGO SECTION
[/td]
[td]
BUTTON 2 - UN-HIDE INDIGO SECTION
[/td]
[/tr]
[tr]
[td]
INDIGO
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
INDIGO
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
INDIGO
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
VIOLET SECTION HEADER ROW
[/td]
[td]
BUTTON 1 - HIDE VIOLET SECTION
[/td]
[td]
BUTTON 2 - UN-HIDE VIOLET SECTION
[/td]
[/tr]
[tr]
[td]
VIOLET
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
VIOLET
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
VIOLET
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
FUCHSIA SECTION HEADER ROW
[/td]
[td]
BUTTON 1 - HIDE FUCHSIA SECTION
[/td]
[td]
BUTTON 2 - UN-HIDE FUCHSIA SECTION
[/td]
[/tr]
[tr]
[td]
FUCHSIA
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
FUCHSIA
[/td]
[td][/td]
[td][/td]
[/tr]
[tr]
[td]
FUCHSIA
[/td]
[td][/td]
[td][/td]
[/tr]
[/TABLE]
The problem is, my code doesn't save any existing filter criteria settings...
So let's say my user has already clicked button 1 for the RED section - which filters column N to exclude RED from the results, so the RED section is hidden. Now my user gets to the GREEN section and clicks button 1 to hide that section - the code should capture any existing filter criteria (in this case, RED is excluded) and modify the filter to now exclude GREEN as well) so the results would be that both the RED and GREEN sections are hidden.
Now let's say the user changes their mind and wants the GREEN section to show up again so they go to click button 2 - the code should capture the existing criteria (in this case RED and GREEN are excluded), add the GREEN back in but leave the RED out.
I've been trying to figure this out for days, and I did find the code below from this thread: http://www.mrexcel.com/forum/excel-questions/333961-capture-autofilter-state.html"]http://www.mrexcel.com/forum/excel-questions/333961-capture-autofilter-state.html[/URL] But being a VBA novice, I can't seem to make heads nor tails of it and modify it for my purposes. Of course, I'm up against a deadline of completing this project by the end of the week, so any help would be greatly appreciated!!!
Private Sub cbSplit_Click()
Dim ws As Worksheet
Dim filterArray()
Dim currentFiltRange As String
Dim col As Integer
Application.ScreenUpdating = False
Set ws = ActiveSheet
'Capture AutoFilter settings
If ws.AutoFilterMode = True Then
With ws.AutoFilter
currentFiltRange = .Range.Address
With .Filters
ReDim filterArray(1 To .Count, 1 To 3)
For col = 1 To .Count
With .Item(col)
If .On Then
filterArray(col, 1) = .Criteria1
If .Operator Then
filterArray(col, 2) = .Operator
If .Operator = xlAnd Or .Operator = xlOr Then
filterArray(col, 3) = .Criteria2
End If
End If
End If
End With
Next col
End With
End With
End If
'Remove AutoFilter
ws.AutoFilterMode = False
'Your code here
'Restore Filter settings
If Not currentFiltRange = "" Then
ws.Range(currentFiltRange).AutoFilter
For col = 1 To UBound(filterArray(), 1)
If Not IsEmpty(filterArray(col, 1)) Then
If filterArray(col, 2) Then
'check if Criteria2 exists and needs to be populated
If filterArray(col, 2) = xlAnd Or filterArray(col, 2) = xlOr Then
ws.Range(currentFiltRange).AutoFilter Field:=col, _
Criteria1:=filterArray(col, 1), _
Operator:=filterArray(col, 2), _
Criteria2:=filterArray(col, 3)
Else
ws.Range(currentFiltRange).AutoFilter Field:=col, _
Criteria1:=filterArray(col, 1), _
Operator:=filterArray(col, 2)
End If
Else
ws.Range(currentFiltRange).AutoFilter Field:=col, _
Criteria1:=filterArray(col, 1)
End If
End If
Next col
End If
Application.ScreenUpdating = True
End Sub
Display More