# Get Filtered Data Range in a Table

Purpose:
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

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 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


Newlines
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.

• You should explicitly declare the return type of your function.

   Dim rowRange As Range

Set filteredRange = rowRange
End If

For Each rowRange In allDataRange.Rows()


Could be:

If includeHeaders Then
End If

Dim rowRange As Range
For Each rowRange In allDataRange.Rows()


This moves rowRange closer to it's first actual usage.

• Good catch on the return type. – Kaz Jul 6 '16 at 15:33

The function name is not as intuitive as it could be.

Something like

GetVisibleTableRange()

is closer to what the function is actually doing (getting all the visible data), but doesn't mesh as well with the default option of excluding headers. The name implies getting the Table Range, rather than just the Data Range.

Not sure if it'll fully fit your use case, but allDataRange.SpecialCells(xlCellTypeVisible) would return a range of only visible cells. Would seem to be more viable than needing to loop through every row of the range.
• This is a good solution if you include error handling. Range.SpecialCells will throw an error if there are no cells of the specified type. – ChrisB Mar 12 '18 at 19:46
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.19787131249904633, "perplexity": 4635.741541386038}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 20, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2019-43/segments/1570986648481.7/warc/CC-MAIN-20191014003258-20191014030258-00216.warc.gz"}