-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAutoFilterActions.vb
More file actions
310 lines (250 loc) · 12.9 KB
/
AutoFilterActions.vb
File metadata and controls
310 lines (250 loc) · 12.9 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
Imports DevExpress.Spreadsheet
Imports System
Imports System.Collections.Generic
Imports System.Drawing
Namespace SpreadsheetDocServerAPIPart2
Public Module AutoFilterActions
Public ApplyFilterAction As Action(Of Workbook) = AddressOf ApplyFilter
Public FilterAndSortBySingleColumnAction As Action(Of Workbook) = AddressOf FilterAndSortBySingleColumn
Public FilterAndSortByMultipleColumnsAction As Action(Of Workbook) = AddressOf FilterAndSortByMultipleColumns
Public FilterNumericByConditionAction As Action(Of Workbook) = AddressOf FilterNumericByCondition
Public FilterTextByConditionAction As Action(Of Workbook) = AddressOf FilterTextByCondition
Public FilterByValueAction As Action(Of Workbook) = AddressOf FilterByValue
Public FilterByMultipleValuesAction As Action(Of Workbook) = AddressOf FilterByMultipleValues
Public FilterDatesByConditionAction As Action(Of Workbook) = AddressOf FilterDatesByCondition
Public FilterMixedDataTypesByValuesAction As Action(Of Workbook) = AddressOf FilterMixedDataTypesByValues
Public Top10FilterValueAction As Action(Of Workbook) = AddressOf Top10FilterValue
Public DynamicFilterValueAction As Action(Of Workbook) = AddressOf DynamicFilterValue
Public FilterAndSortByColorAction As Action(Of Workbook) = AddressOf FilterAndSortByColor
Public FilterByBackgroundColorAction As Action(Of Workbook) = AddressOf FilterByBackgroundColor
Public FilterByFillColorAction As Action(Of Workbook) = AddressOf FilterByFillColor
Public FilterByFontColorAction As Action(Of Workbook) = AddressOf FilterByFontColor
Public ReapplyFilterValueAction As Action(Of Workbook) = AddressOf ReapplyFilterValue
Public ClearFilterAction As Action(Of Workbook) = AddressOf ClearFilter
Public DisableFilterAction As Action(Of Workbook) = AddressOf DisableFilter
Private Sub ApplyFilter(ByVal workbook As Workbook)
' #Region "#ApplyFilter"
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' Enable filtering for the "B2:E23" cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' #End Region ' #ApplyFilter
End Sub
Private Sub FilterAndSortBySingleColumn(ByVal workbook As Workbook)
' #Region "#FilterAndSortBySingleColumn"
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' Enable filtering for the "B2:E23" cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' Sort data in the "B2:E23" range
' in descending order by column "A".
worksheet.AutoFilter.SortState.Sort(0, True)
' #End Region ' #FilterAndSortBySingleColumn
End Sub
Private Sub FilterAndSortByMultipleColumns(ByVal workbook As Workbook)
' #Region "#FilterAndSortByMultipleColumns"
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' Enable filtering for the "B2:E23" cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' Sort data in the "B2:E23" range
' in descending order by columns "A" and "C".
Dim sortConditions As New List(Of SortCondition)()
Dim color As Color = worksheet("D12").Font.Color
sortConditions.Add(New SortCondition(0, True))
sortConditions.Add(New SortCondition(2, color, False))
worksheet.AutoFilter.SortState.Sort(sortConditions)
' #End Region ' #FilterAndSortByMultipleColumns
End Sub
Private Sub FilterNumericByCondition(ByVal workbook As Workbook)
' #Region "#FilterNumbersByCondition"
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' Enable filtering for the "B2:E23" cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' Filter values in the "Sales" column that are in a range from 5000$ to 8000$.
Dim sales As AutoFilterColumn = worksheet.AutoFilter.Columns(2)
sales.ApplyCustomFilter(5000, FilterComparisonOperator.GreaterThanOrEqual, 8000, FilterComparisonOperator.LessThanOrEqual, True)
' #End Region ' #FilterNumbersByCondition
End Sub
Private Sub FilterTextByCondition(ByVal workbook As Workbook)
' #Region "#FilterTextByCondition"
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' Enable filtering for the "B2:E23" cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' Filter values in the "Product" column that contain "Gi" and include empty cells.
Dim products As AutoFilterColumn = worksheet.AutoFilter.Columns(1)
products.ApplyCustomFilter("*Gi*", FilterComparisonOperator.Equal, FilterValue.FilterByBlank, FilterComparisonOperator.Equal, False)
' #End Region ' #FilterTextByCondition
End Sub
Private Sub FilterByValue(ByVal workbook As Workbook)
' #Region "#FilterBySingleValue"
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' Enable filtering for the "B2:E23" cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' Filter data in the "Product" column by a specific value.
worksheet.AutoFilter.Columns(1).ApplyFilterCriteria("Mozzarella di Giovanni")
' #End Region ' #FilterBySingleValue
End Sub
Private Sub FilterByMultipleValues(ByVal workbook As Workbook)
' #Region "#FilterByMultipleValues"
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' Enable filtering for the "B2:E23" cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' Filter data in the "Product" column by an array of values.
worksheet.AutoFilter.Columns(1).ApplyFilterCriteria(New CellValue() { "Mozzarella di Giovanni", "Gorgonzola Telino" })
' #End Region ' #FilterByMultipleValues
End Sub
Private Sub FilterDatesByCondition(ByVal workbook As Workbook)
' #Region "#FilterDatesByCondition "
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' Enable filtering for the "B2:E23" cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' Filter values in the "Reported Date" column
' to display dates that are between June 1, 2014 and February 1, 2015.
worksheet.AutoFilter.Columns(3).ApplyCustomFilter(New Date(2014, 6, 1), FilterComparisonOperator.GreaterThanOrEqual, New Date(2015, 2, 1), FilterComparisonOperator.LessThanOrEqual, True)
' #End Region ' #FilterDatesByCondition
End Sub
Private Sub FilterMixedDataTypesByValues(ByVal workbook As Workbook)
' #Region "#FilterMixedDataByValues "
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' Enable filtering for the "B2:E23" cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' Create date grouping item to filter January 2015 dates.
Dim groupings As IList(Of DateGrouping) = New List(Of DateGrouping)()
Dim dateGroupingJan2015 As New DateGrouping(New Date(2015, 1, 1), DateTimeGroupingType.Month)
groupings.Add(dateGroupingJan2015)
' Filter data in the "Reported Date" column
' to display values reported in January 2015.
worksheet.AutoFilter.Columns(3).ApplyFilterCriteria("gennaio 2015", groupings)
' #End Region ' #FilterMixedDataByValues
End Sub
Private Sub Top10FilterValue(ByVal workbook As Workbook)
' #Region "#TopTenFilter "
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' Enable filtering for the "B2:E23" cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' Apply a filter to the "Sales" column to display the top ten values.
worksheet.AutoFilter.Columns(2).ApplyTop10Filter(Top10Type.Top10Items, 10)
' #End Region ' #TopTenFilter
End Sub
Private Sub DynamicFilterValue(ByVal workbook As Workbook)
' #Region "#DynamicFilter"
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' Enable filtering for the "B2:E23" cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' Apply a dynamic filter to the "Sales" column
' to display only values that are above the average.
worksheet.AutoFilter.Columns(2).ApplyDynamicFilter(DynamicFilterType.AboveAverage)
' Apply a dynamic filter to the "Reported Date" column
' to display values reported this year.
worksheet.AutoFilter.Columns(3).ApplyDynamicFilter(DynamicFilterType.ThisYear)
' #End Region ' #DynamicFilter
End Sub
Private Sub FilterAndSortByColor(ByVal workbook As Workbook)
' #Region "#FilterAndSortByColor"
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' Enable filtering for the "B2:E23" cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' Sort data in the "B2:E23" range
' in descending order by column "D".
Dim color As Color = worksheet("D12").Font.Color
worksheet.AutoFilter.SortState.Sort(2, color, False)
' #End Region ' #FilterAndSortByColor
End Sub
Private Sub FilterByBackgroundColor(ByVal workbook As Workbook)
' #Region "#FilterByBackgroundColor"
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' Enable filtering for the "B2:E23" cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' Filter values in the "Products" column by background color.
Dim products As AutoFilterColumn = worksheet.AutoFilter.Columns(1)
products.ApplyFillColorFilter(worksheet("C12").FillColor)
' #End Region ' #FilterByBackgroundColor
End Sub
Private Sub FilterByFillColor(ByVal workbook As Workbook)
' #Region "#FilterByFillColor"
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' Enable filtering for the "B2:E23" cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' Filter values in the "Products" column by fill color.
Dim products As AutoFilterColumn = worksheet.AutoFilter.Columns(1)
products.ApplyFillFilter(worksheet("C10").Fill)
' #End Region ' #FilterByFillColor
End Sub
Private Sub FilterByFontColor(ByVal workbook As Workbook)
' #Region "#FilterByFontColor"
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' Enable filtering for the "B2:E23" cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' Filter values in the "Sales" column by font color.
Dim products As AutoFilterColumn = worksheet.AutoFilter.Columns(2)
products.ApplyFontColorFilter(worksheet("D10").Font.Color)
' #End Region ' #FilterByFontColor
End Sub
Private Sub ReapplyFilterValue(ByVal workbook As Workbook)
' #Region "#ReapplyFilter "
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' Enable filtering for the "B2:E23" cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' Filter values in the "Sales" column that are greater than 5000$.
worksheet.AutoFilter.Columns(2).ApplyCustomFilter(5000, FilterComparisonOperator.GreaterThan)
' Change data and reapply the filter.
worksheet("D3").Value = 5000
worksheet.AutoFilter.ReApply()
' #End Region ' #ReapplyFilter
End Sub
Private Sub ClearFilter(ByVal workbook As Workbook)
' #Region "#ClearFilter"
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' Enable filtering for the "B2:E23" cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' Filter values in the "Sales" column that are greater than 5000$.
worksheet.AutoFilter.Columns(2).ApplyCustomFilter(5000, FilterComparisonOperator.GreaterThan)
' Clear the filter.
worksheet.AutoFilter.Clear()
' #End Region ' #ClearFilter
End Sub
Private Sub DisableFilter(ByVal workbook As Workbook)
' #Region "#DisableFilter"
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' Enable filtering for the "B2:E23" cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' Disable filtering for the entire worksheet.
worksheet.AutoFilter.Disable()
' #End Region ' #DisableFilter
End Sub
End Module
End Namespace