-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathMod_Autofilter.bas
180 lines (163 loc) · 6.72 KB
/
Mod_Autofilter.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
Attribute VB_Name = "Mod_Autofilter"
Option Explicit
Option Base 1
Sub AutoFilter_Clear(Optional stringWksName As String)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' this subroutine clears the autofilter of values and checks to see if there is a filter
'
' Requirements:
' None
'
' Inputs:
' stringWksName
' Type: string
' Desc: worksheet name to activate
'
' Important Info:
' sheet must be activated or selected
'
' Return:
' None
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''’’'''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''’’'''
' declare
Dim wks_current As Worksheet
Dim filtersCollection As Filters
Dim filterLoop As Filter
' start
' activate sheet if passed
If stringWksName = Empty Then
Set wks_current = Worksheets(ActiveSheet.Name)
Else
Set wks_current = Worksheets(stringWksName)
End If
' turn on autofilter & filter newest to oldest
If IsEmpty(Range("A1").Value) = False Then
If wks_current.Range("A1").AutoFilterMode = False Then wks_current.Range("A1").Autofilter Else ' do nothing
' clear filter values
' clear all filter values and show all data
If wks_current.AutoFilterMode = True Then
Set filtersCollection = wks_current.Autofilter.Filters
If filtersCollection.Count > 0 Then
For Each filterLoop In filtersCollection
If filterLoop.On = True Then
wks_current.ShowAllData
Exit For
Else
End If
Next
Else
End If
Else
End If
Else
End If
End Sub
Sub AutoFilter_OnOff(wksWorksheet As Worksheet, rngCell As Range, boolTurnOn As Boolean)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' this subroutine will turn the autofilter on or off
'
' Requirements:
' None
'
' Inputs:
' wksWorksheet
' Type: worksheet object
' Desc: the worksheet to turn the autofilter on or off
'
' rngCell
' Type: range object, worksheet cell
' Desc: the cell to toggle the autofilter
'
' boolTurnOn
' Type: boolean
' Desc: flag to turn the autofilter on or off
'
' Important Info:
' None
'
' Return:
' None
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''’’'''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''’’'''
' declare objects
Dim wks_autofilter As Autofilter
Dim range_cell As Range
' set objects
Set wks_autofilter = wksWorksheet.Autofilter
Set range_cell = wksWorksheet.Range(rngCell.Address(False, False))
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''’’'''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''’’'''
'
' begin
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''’’'''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''’’'''
' if autofilter is on and it should be on
If Not wks_autofilter Is Nothing And boolTurnOn = True Then
' do nothing
' if autofilter is off and it should be on
ElseIf wks_autofilter Is Nothing And boolTurnOn = True Then
range_cell.Autofilter
' if autofilter is on and it should be off
ElseIf Not wks_autofilter Is Nothing And boolTurnOn = False Then
range_cell.Autofilter
Else ' do nothing
End If
' reset objects
Set wks_autofilter = Nothing
Set range_cell = Nothing
End Sub
Sub Autofilter_SingleSort(wksWorksheet As Worksheet, stringSortRange As String, boolDescending As Boolean)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' this subroutine will sort a worksheet from a stringSortRange. this sub will only sort on one sort field not multiple
'
' Requirements:
' ClearAutoFilter()
'
' Inputs:
' wksWorksheet
' Type: worksheet
' Desc: worksheet to be sorted
'
'
' stringSortRange
' Type: string
' Desc: range to be sorted
'
' boolDescending
' Type: boolDescending
' Desc: True = descending, False = ascending
'
' Important Info:
' this subroutine only sorts one sort field
' this subroutine will clear all the sortfields
'
' Return:
' None
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''’’'''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''’’'''
' declare
Dim varaintOrder As Variant
' initialize
varaintOrder = xlDescending
' start
' test for ascending
If boolDescending = False Then varaintOrder = xlAscending Else ' do nothing
' clear autofilter
Call AutoFilter_Clear(wksWorksheet.Name)
' begin sort
wksWorksheet.Rows.Hidden = False
wksWorksheet.Autofilter.Sort.SortFields.Clear
wksWorksheet.Autofilter.Sort.SortFields.Add Key:=Range(stringSortRange), SortOn:=xlSortOnValues, Order:=varaintOrder, DataOption:=xlSortNormal
With wksWorksheet.Autofilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub