In my workbook, I give the users the ability to turn on filtering for a few columns of a worksheet. I have other code that exports the worksheet to a new file. Sometimes, the export doesn't work correctly if the user has filtered enabled AND has an active filter applied. So I'm trying to do four things:
1. Notate the specifics of the applied filter.
2. Turn filtering off.
3. Run some other code.
4. Turn filtering back on and reapply the filter that was notated in step 1.
I ran across this thread when researching how to do this:
Capture Autofilter state
The very last answer seemed promising, so I am trying what that person suggested. I modified their code a little bit to have two subs: one that does #1 & #2 above, and the does #4 above. The code that does #4 isn't working, and I have no idea why. Here's the code.
For Steps # 1 & 2
For Step #4
And here's the relevant code in the main sub that calls both of the above.
The sheet is exported properly, but filtering is not reapplied afterwards. Any help to understand what I need to do to make it work would be greatly appreciated.
1. Notate the specifics of the applied filter.
2. Turn filtering off.
3. Run some other code.
4. Turn filtering back on and reapply the filter that was notated in step 1.
I ran across this thread when researching how to do this:
Capture Autofilter state
The very last answer seemed promising, so I am trying what that person suggested. I modified their code a little bit to have two subs: one that does #1 & #2 above, and the does #4 above. The code that does #4 isn't working, and I have no idea why. Here's the code.
For Steps # 1 & 2
VBA Code:
Sub POLog_TurnOffFilteringIfNeeded(FilterApplied As Boolean, cv As CustomView)
With ThisWorkbook.Sheets("Purchase Order Log")
If .AutoFilterMode = True And .FilterMode = True Then FilterApplied = True
If FilterApplied = True Then
Set cv = ThisWorkbook.CustomViews.Add(ViewName:="TempName", RowColSettings:=True)
.AutoFilterMode = False
End If
End With
End Sub
For Step #4
VBA Code:
Sub POLog_TurnOnFilteringIfNeeded(FilterApplied As Boolean, cv As CustomView)
If FilterApplied = True Then
If Not cv Is Nothing Then
cv.Show
cv.Delete
End If
End If
End Sub
And here's the relevant code in the main sub that calls both of the above.
VBA Code:
Dim FilterApplied As Boolean
Dim cv As CustomView
Call POLog_TurnOffFilteringIfNeeded(FilterApplied, cv)
[... other code here that exports the sheet ...]
Call POLog_TurnOnFilteringIfNeeded(FilterApplied, cv)
Set cv = Nothing
The sheet is exported properly, but filtering is not reapplied afterwards. Any help to understand what I need to do to make it work would be greatly appreciated.