Consulting

Results 1 to 7 of 7

Thread: Select range of visible cells

  1. #1
    VBAX Newbie
    Joined
    Oct 2015
    Posts
    3
    Location

    Select range of visible cells

    Hello all,

    I need a little help.

    I'm trying to copy the top 5 lines of a filtered table.


    Range("A3").Select

    'Next visible cell start
    ActiveCell.Offset(1, 0).Activate
    Do While ActiveCell.EntireRow.Hidden = True
    ActiveCell.Offset(1, 0).Activate
    Loop
    'Next visible cell end

    ActiveCell.Offset(1, 0).Resize(5, 1).Copy
    A3 is the top of the table. The first loop takes it down to the first visible cell.

    but I can't figure out how to get the offset command to only include visible fields. In this case I have something in A19 and then nothing else until A35 so this command only copies 1 line instead of 5.

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome to the forum...

    so the headers are in row 3?

    run below code and open Immediate Window in VBE to see why Resize, etc fail...


    Sub FilteredRowNumbers()
        With Worksheets("MySheet") 'change MySheet to suit
            .AutoFilterMode = False
            .Range("A3").AutoFilter Field:=2, Criteria1:="=MyCrit"
            For Each cll In .UsedRange.Columns(1).SpecialCells(xlCellTypeVisible)
                Debug.Print cll.Row
            Next
            .AutoFilterMode = False
        End With
    End Sub
    Last edited by mancubus; 10-07-2015 at 12:33 AM. Reason: typo
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    below code copies all the auto filter range and the clears the cells below top Nth row.

    Sub vbax_53926_Copy_Top_N_Rows_AutoFilterRange()
        Dim TopN As Long
        
        TopN = 5
        
        With Worksheets("MySheet") 'change MySheet to suit
            .AutoFilterMode = False
            .Range("A3").AutoFilter Field:=2, Criteria1:="=MyCrit"
            .AutoFilter.Range.Copy Destination:=Worksheets("MySheet2").Range("A1")
            .AutoFilterMode = False
        End With
        
        Worksheets("MySheet2").UsedRange.Offset(TopN).Clear
    End Sub
    Last edited by mancubus; 10-07-2015 at 12:34 AM.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    VBAX Newbie
    Joined
    Oct 2015
    Posts
    3
    Location
    Quote Originally Posted by mancubus View Post
    below code copies all the auto filter range and the clears the cells below top Nth row.

    Sub vbax_53926_Copy_Top_N_Rows_AutoFilterRange()
        Dim TopN As Long
        
        TopN = 5
        
        With Worksheets("MySheet") 'change MySheet to suit
            .AutoFilterMode = False
            .Range("A3").AutoFilter Field:=2, Criteria1:="=MyCrit"
            .AutoFilter.Range.Copy Destination:=Worksheets("MySheet2").Range("A1")
            .AutoFilterMode = False
        End With
        
        Worksheets("MySheet2").UsedRange.Offset(TopN).Clear
    End Sub

    Hi Thanks for the response

    In response to your first post. None of the original script is throwing an actual error. It is just copying and pasting the wrong thing. This portion

    ActiveCell.Offset(1, 0).Resize(5, 1).Copy

    is going down copying the next 5 cells down, but if some of those cells are hidden it copies those and not just the visible cells in the filtered table.



    The second code you sent with the TopN = 5 statement gets me closer to what I want, but I only need the one column out of the table and not the whole table.

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome.
    pls dont quote the whole message or code.

    yes, it does not throw an error; it just fails to do what you are after.

    change
    .AutoFilter.Range.Copy

    to
    .AutoFilter.Range.Columns(1).Copy

    to copy Column A of filtered table.

    as you may know, Columns(1) is Column A, Columns(2) is Column B, Columns(5) is Column E, etc.
    Last edited by mancubus; 10-07-2015 at 06:58 AM. Reason: typo
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  6. #6
    VBAX Newbie
    Joined
    Oct 2015
    Posts
    3
    Location
    Perfect! That got me where I needed to be. I had the longest workaround. You just cut processing time of the macro down by like 45 seconds. Thank you!

  7. #7
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome.
    please mark the thread as solved from thread tools for future references...
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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