Jeffrey Mahoney
Well-known Member
- Joined
- May 31, 2015
- Messages
- 3,142
- Office Version
- 365
- Platform
- Windows
I created a tool to allow the user to select multiple items within a single column that they want filtered. The problem was that I would try to add items to the filter and I would inevitably forget to click "Add current selection to filter" and then erase the previous items I had selected. One of the features I added in the code below is to also select the negative/positive opposite. This is handy for financial people that are dealing with a large number of repeating accruals.
In the code below, the filtered range is found either by 1) the range already established by the filter, 2) the range in a structured Excel table, or 3) the CurrentRegion.
The problem is with CurrentRegion. It may select all the cells with data. In most of my cases, my tables may start on row 5 with some information above the table including column totals.
Does anybody know a cleaner way to select only the data within the table (non-structured). Example below:
In the code below, the filtered range is found either by 1) the range already established by the filter, 2) the range in a structured Excel table, or 3) the CurrentRegion.
The problem is with CurrentRegion. It may select all the cells with data. In most of my cases, my tables may start on row 5 with some information above the table including column totals.
Does anybody know a cleaner way to select only the data within the table (non-structured). Example below:
00KRC 20230717.XLSX | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
A | C | E | F | G | H | I | J | |||||
1 | Exported from Application on 17-Jul-2023 08:43:31 | |||||||||||
2 | Posted | |||||||||||
3 | Project: ZAP1234 Some Project Name Object: 12345 12345 | 70,010,124.60 | Dec-2020 - Jul-2023 | |||||||||
4 | CBS Path ID | Description | Cost | Date | Data Source | WO | PO | YearMonth | ||||
5 | ZAP-1234|987123|987|987123|987123|987123|C337536 | SAP PR4 Postings for 07/14/2023 | 1,234.00 | 14-Jul-2023 | SAPPR4 | 123456 | C123456 | 202307 | ||||
6 | ZAP-1234|987123|987|987123|987123|987123|C322431 | SAP PR4 Postings for 07/14/2023 | 1,349.48 | 14-Jul-2023 | SAPPR4 | 123456 | C123456 | 202307 | ||||
7 | ZAP-1234|987123|987|987123|999999|987123|C338474 | SAP PR4 Postings for 07/14/2023 | 1,464.96 | 14-Jul-2023 | SAPPR4 | 123456 | C123456 | 202307 | ||||
8 | ZAP-1234|987123|987|987123|999999|987123|C340367 | SAP PR4 Postings for 07/14/2023 | 1,580.44 | 14-Jul-2023 | SAPPR4 | 123456 | C123456 | 202307 | ||||
Posted |
Cell Formulas | ||
---|---|---|
Range | Formula | |
E3 | E3 | =SUBTOTAL(109,E5:E1095) |
F3 | F3 | =TEXT(SUBTOTAL(105,F5:F1095),"mmm-yyyy")&" - "&TEXT(SUBTOTAL(104,F5:F1095),"mmm-yyyy") |
E6:E8 | E6 | =+E5+115.48 |
VBA Code:
'This will filter multiple selected items in a single column that you select
'You may select multiple cells and ranges within the same column
'You may run this again with a different set of criteria on the same or a different column
'Autofilter will be activated if off
'Filter Criteria requires the number formatting to be correct with all commas and dollar signs
'This assumes the number format is the same for the whole column
'This will add the positive/negative opposite of a number in the criteria
'This works with text just the same
Sub AutoFilterItems()
Dim Rng As Range
Dim Col As Range
Dim Sel As Range
Dim Sht As Worksheet
Dim Fld As Long
Dim Cel As Range
Dim Ary As Variant
Dim aStr As String
Dim RngArea As Range
Dim NF As String
Dim X As Long
Dim Val As Double
Dim vStr As String
Dim Changes As Long
Dim vErr As Boolean
Dim App As Application
Dim WF As WorksheetFunction
Dim LO As ListObject
Set Sht = ActiveSheet
Set Sel = Selection
Set Col = Sel.EntireColumn
If Col.Columns.Count > 1 Then 'User selected more than one column
MsgBox "Please only select items in one column to filter"
Exit Sub
End If
'--------- Find the range that will be filtered ---------------------------------
On Error Resume Next
Set Rng = Sht.AutoFilter.Range 'Autofilter must be on
On Error GoTo 0
If Rng Is Nothing Then
Set LO = Sel.ListObject 'Structured Excel Table
If Not LO Is Nothing Then Set Rng = LO.DataBodyRange
End If
If Rng Is Nothing Then 'Current Region is less accurate
Set Rng = Sel.CurrentRegion
Rng.AutoFilter
End If
'---------------------------------------------------------------------------------
Fld = Col.Column - Rng.Resize(1, 1).Column + 1 'Field number in filtered range
NF = Sel.Areas(1).Resize(1, 1).NumberFormat 'Get number format of first cell
'load all cells into string
For Each RngArea In Sel.Areas 'Even ranges of cells
For Each Cel In RngArea
vStr = Cel.Value
If NF <> "General" Then vStr = Format(Cel, NF) 'Format function doesn't like General
aStr = aStr & Chr(1) & vStr
'----- Section for adding Opposites -----
vErr = False 'reset vErr
On Error GoTo NumberError 'Turn on error checking
Val = Cel.Value 'Change to number - may get an error if text
On Error GoTo 0 'turn off error checking
If vErr = False Then 'is number?
vStr = Val * -1
If NF <> "General" Then vStr = Format(Val * -1, NF)
aStr = aStr & Chr(1) & vStr 'add oposite value to string
End If
'-------------------------------------------
Next Cel
Next RngArea
Ary = Split(Mid(aStr, 2), Chr(1)) 'load into array separated by spaces
Rng.AutoFilter Field:=Fld, Criteria1:=Ary, Operator:=xlFilterValues 'Filter sheet
Exit Sub
NumberError:
vErr = True
Resume Next
End Sub