+ Reply to Thread
Results 1 to 14 of 14

Filtered data to exclude header row

  1. #1
    Forum Contributor
    Join Date
    11-05-2014
    Location
    Charlotte
    MS-Off Ver
    2013
    Posts
    181

    Filtered data to exclude header row

    Hi All!

    I'm trying to update my code below to only copy the visible cells and exclude the header row.

    When there is criteria that meets the filter requirements, the code works fine (header is not copied), but when there is no criteria that meets the filter requirements, the header row gets selected. Why is this? And how am I able to update my code to exclude the header row?

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Filtered data to exclude header row

    Short and dirty way is to put in on error resume next.
    OR

    Please Login or Register  to view this content.
    This will make the code to skip and not copy any data if the criteria are not met.

  3. #3
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: Filtered data to exclude header row

    I occasionally deal with this by adding one to LastRow.

    Please Login or Register  to view this content.
    BSB

  4. #4
    Forum Contributor
    Join Date
    11-05-2014
    Location
    Charlotte
    MS-Off Ver
    2013
    Posts
    181

    Re: Filtered data to exclude header row

    Thanks, but the code is not working for me.

    LastRow isn't the count of the row, but the Row number it's on. So, for example, if I had my data filtered for 3 only rows, Row 7, 14, and 29, the LastRow=29.

  5. #5
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Filtered data to exclude header row

    I should have include equal to
    If LastRow >= 2 Then
    When you filter data and criteria are not met, there is no visible cells to copy. In such situation, the code will not copy, nor error. If there are filtered cells and count of rows are >=2 the code copies the filtered cells.
    Difficult to show you in text format, please attach the code with the sample and will correct it.

  6. #6
    Forum Contributor
    Join Date
    11-05-2014
    Location
    Charlotte
    MS-Off Ver
    2013
    Posts
    181

    Re: Filtered data to exclude header row

    That worked. Thanks, AB33!

  7. #7
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Filtered data to exclude header row

    Assuming you use autofilter have you tried using the Offset function?

    Please Login or Register  to view this content.
    Alf

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Filtered data to exclude header row

    ALf,
    A2 will do.
    The issue is if the criteria fail, filter copies the header. I would normally expect to get an error if criteria fail, as there are not visible cells to copy, hence for skipping the error.

    Filter is very clunky function and seems to have some bugs (From my experience).

  9. #9
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Filtered data to exclude header row

    With headings in row 1 and no data found using autofilter settings it seems like the Offset(1, 0) command overrides the SpecialCells(xlCellTypeVisible) setting so a copy command it will happily copy 1 empty row i.e. row 2 in a case like this.

    Alf

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Filtered data to exclude header row

    What happens if the criteria are not found? Will the offset still works? How can VBA copies data if it could not find any visible cells, hence the error? The offset is just a way of skipping the first cell in the range.

  11. #11
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Filtered data to exclude header row

    Step through the macro "Tester" and you will see what I mean. The criteria in filed 1 is "paul" a value not found in data range so the offset command copies the
    first row outside the filtered range i.e. row 6 which makes sense as this is outside autofilter range and the "SpecialCells(xlCellTypeVisible).Copy" command can happily
    be ignored.

    Alf
    Attached Files Attached Files

  12. #12
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Filtered data to exclude header row

    Alf,
    Okay, it is another way of skipping the error.
    If I want to copy, including a header, I have changed the offset to 0
    ActiveSheet.AutoFilter.Range.Offset(0, 0).SpecialCells(xlCellTypeVisible).Copy
    If the match is found, it works and does not make any difference, but imagine there is no match and I do not want to copy the header. Mine skips it, but your code copies the header.

  13. #13
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Filtered data to exclude header row

    Funny I thought the OP's problem was
    to update my code below to only copy the visible cells and exclude the header row
    that is why one would use the Offset(1, 0). Cant see why one would use Offset(0, 0) unless one wanted to include header row.

    Alf

  14. #14
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Filtered data to exclude header row

    Yes, you are right as far as the OP is concerned, "BUT" and then I do not want to drag the issue in to Ifs and Buts and I rest my case.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Exclude Filtered Rows in SUBTOTAL
    By callie64 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-10-2016, 03:34 PM
  2. Exclude header row from dump
    By rlowe in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-04-2013, 10:52 PM
  3. export filtered data (including header row) to new workbook, maintain column width
    By ovetcobo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-26-2013, 03:47 PM
  4. [SOLVED] Copy a specific column data of filtered output without header
    By uvaidya in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 11-05-2012, 09:21 AM
  5. Find first data row after header when data is filtered.
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 10-22-2012, 07:40 PM
  6. Finging first row of a given column that has data in it.......exclude header
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-14-2011, 02:23 PM
  7. Copy filtered data, not header
    By melbri in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-07-2010, 07:22 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1