patternMinor
Get Filtered Data Range in a Table
Viewed 0 times
rangegetdatatablefiltered
Problem
Purpose:
Given a Worksheet Table (
Method:
Loop through each row in the table.
If row is visible, add it to a
Return
Specific Areas of Interest:
Is the function name sufficiently intuitive?
Is there an easier way to achieve the desired results?
Are there edge cases I haven't accounted for?
Function:
Given a Worksheet Table (
ListObject) that may, or may not, have a filter currently applied to it, return a range consisting only of the filtered data.Method:
Loop through each row in the table.
If row is visible, add it to a
filteredRange via Union().Return
filteredRange.Specific Areas of Interest:
Is the function name sufficiently intuitive?
Is there an easier way to achieve the desired results?
Are there edge cases I haven't accounted for?
Function:
Public Function GetFilteredTableRange(ByRef targetTable As ListObject, Optional ByVal includeHeaders As Boolean = False)
'/ given a table, return a range object that contains only those rows which are visible
'/ Do this by looping through tableRows, adding all visible rows to a unionRange
Dim allDataRange As Range
Set allDataRange = targetTable.DataBodyRange
Dim filteredRange As Range
Dim rowRange As Range
If includeHeaders Then
Set rowRange = targetTable.HeaderRowRange
Set filteredRange = rowRange
End If
For Each rowRange In allDataRange.Rows()
If rowRange.EntireRow.Hidden = False Then
If filteredRange Is Nothing Then
Set filteredRange = rowRange
Else
Set filteredRange = Union(filteredRange, rowRange)
End If
End If
Next rowRange
Set GetFilteredTableRange = filteredRange
End FunctionSolution
Public Function GetFilteredTableRange(ByRef targetTable As ListObject, Optional ByVal includeHeaders As Boolean = False)
'/ given a table, return a range object that contains only those rows which are visible
'/ Do this by looping through tableRows, adding all visible rows to a unionRangeNewlines
are
cheap.
In Python, you always aim to keep your lines shorter than 80 characters long. I personally think it's a good practice to keep that size as a guideline in other languages as well. Your first line is 120, 1.5x my proposed maximum.
Added benefit of trying to stick with a maximum is it keeps you sharp. You're forced to think whether it's absolutely necessary for that line to be that long. Long lines are not fun to read, so try not to make them longer than necessary.
Public Function GetFilteredTableRange( _
ByRef targetTable As ListObject, _
Optional ByVal includeHeaders As Boolean = False _
)
'/ Given a table, return a range object with only the visible rows.
'/ Do this by looping through tableRows,
'/ adding all visible rows to a unionRange.That's how I'd do it, but I'm not up to par with VBA coding standards.
Code Snippets
Public Function GetFilteredTableRange(ByRef targetTable As ListObject, Optional ByVal includeHeaders As Boolean = False)
'/ given a table, return a range object that contains only those rows which are visible
'/ Do this by looping through tableRows, adding all visible rows to a unionRangePublic Function GetFilteredTableRange( _
ByRef targetTable As ListObject, _
Optional ByVal includeHeaders As Boolean = False _
)
'/ Given a table, return a range object with only the visible rows.
'/ Do this by looping through tableRows,
'/ adding all visible rows to a unionRange.Context
StackExchange Code Review Q#134050, answer score: 6
Revisions (0)
No revisions yet.