+ Reply to Thread
Results 1 to 5 of 5

Apply VBA code to filtered rows

Hybrid View

  1. #1
    Registered User
    Join Date
    05-04-2012
    Location
    Portugal
    MS-Off Ver
    Excel 2007
    Posts
    10

    Question Apply VBA code to filtered rows

    Hi all,

    As you can see here ( https://docs.google.com/spreadsheet/...1E&pli=1#gid=0 ), I have a spreadsheet that was filtered manually, and now I want to apply some VBA code I made, only to the filtered rows, presented in the link. How can I do this?

    By the way, the code goes like this (the range present on the code is an old one, I want to change that for the filtered rows on the previous link):
    Sub SortRows()
        Dim RgToSort As Range
        Dim RgRow As Range
        Application.ScreenUpdating = False
        
        Set RgToSort = Range(Range("B1:C42"), Range("B1:C42").End(xlDown))
        
        For Each RgRow In RgToSort.Rows
            RgRow.Sort Key1:=Range(RgRow.Item(1).Address), Order1:=xlAscending, Orientation:=xlLeftToRight, OrderCustom:=1
        Next
        Application.ScreenUpdating = True
    End Sub
    Thanks in advance,
    Joćo Fernandes

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Apply VBA code to filtered rows

    Hi,

    A brief test I've just done shows that if you sort a filtered range, only the visible rows have been sorted after you remove the filter. I was somewhat surprised but there you go.

    Hence just use the standard VBA sort syntax that you showed in your example.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    05-04-2012
    Location
    Portugal
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Apply VBA code to filtered rows

    Thanks for your quick and useful reply, Richard. Ok, I got it. I've just tested that code again, and in order to sort all the rows I wanted, I had to change the range to "B1:C61", to include all the rows I wanted in that Google Doc spreadsheet. One of the doubts I had was how to refer to the range (using the unfiltered cell references vs. filtered ones). I'm using the unfiltered ones, like in the example, and it's working smooth.

    Regards,
    Joćo Fernandes
    Last edited by jaff84; 06-01-2012 at 10:01 AM.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Apply VBA code to filtered rows

    Hi,

    As you've discovered it's working OK anyway. But for future reference if you ever want to address just the filtered rows in a range of data, e.g. for copying, then you could use the following syntax.

    Range("MyRange").SpecialCells(xlCellTypeVisible).Copy

    Regards

  5. #5
    Registered User
    Join Date
    05-04-2012
    Location
    Portugal
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Apply VBA code to filtered rows

    Hi,

    As you've discovered it's working OK anyway. But for future reference if you ever want to address just the filtered rows in a range of data, e.g. for copying, then you could use the following syntax.

    Range("MyRange").SpecialCells(xlCellTypeVisible).Copy

    Regards
    Thank you, that will make it general, just as I wanted, since I'll need to use it in different ranges. I'll try that.

    Thanks again,
    Joćo

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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