+ Reply to Thread
Results 1 to 3 of 3

Apply Macro to Filtered List

Hybrid View

  1. #1
    Registered User
    Join Date
    01-21-2015
    Location
    California, USA
    MS-Off Ver
    2013
    Posts
    35

    Apply Macro to Filtered List

    Hello all,

    Currently I have two macros where two columns are of importance: one column has names, the other has numbers (both columns have text headers).

    The code for the first macro is this:
    Sub FilterDoubles()
    
    selection.AutoFilter
    
        ActiveSheet.Range("$A$1:$CI$14125").AutoFilter Field:=17, Criteria1:=Array( _
            "name1", "name2", "name3", "name4", _
            "name5", "name6", "name7", "name8", _
            "name9", "name10", "name11", "name12", _
            "name13", "name14"), Operator:=xlFilterValues
    
    
    End Sub
    So this one works the way I want it to (I had to edit the real names before posting here). It filters my list by the names that I've given it. After it filters my list, I manually select the second column with numbers and use my second macro.

    The second macro is this:

    Sub DivideBy2()
    
    For Each cell In selection.Cells
        If cell.Value > 0 Then
            cell.Value = cell.Value / 2
        End If
    Next
    
    End Sub
    The problem is that the second macro seems to be applied to ALL cells in the range from the first cell I selected to the last cell I selected (including all cells that were supposed to have been filtered out), and not just the ones that I've selected/filtered for. Any ideas?

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Apply Macro to Filtered List

    Hello YOO629,

    In VBA 99 percent of the time you will never have to use Select or Activate in your code. To help you get the code working, you should post a copy of your workbook.

    Please redact or change any personal, sensitive, or confidential information before you post the workbook.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    01-21-2015
    Location
    California, USA
    MS-Off Ver
    2013
    Posts
    35

    Re: Apply Macro to Filtered List

    Thanks for the reply Leith. I took your advice and changed the selection.autofilter to cells.autofilter

    Fortunately, I managed to find a solution to my problem online.

    I'll share it here for everyone else:

    Sub DivideBy2()
    Dim rng As Range
    
    Set rng = Range("A2", Range("A2").End(xlDown)).Cells.SpecialCells(xlCellTypeVisible)
    
    For Each cell In rng
        cell.Value = cell.Value / 2
    Next
    
    End Sub
    This code will go through a filtered list in column A starting from cell A2 and down and divide each cell value by 2. note that in my case, all entries in column A are numbers except for cell A1, which is a column header.

+ 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. Apply Macro to a list of sheets
    By Fener in forum Non English Excel
    Replies: 1
    Last Post: 02-18-2015, 04:12 PM
  2. Replies: 7
    Last Post: 09-22-2013, 03:40 AM
  3. VBA to Loop through list of sheets and apply macro
    By hungryhobo in forum Excel Programming / VBA / Macros
    Replies: 24
    Last Post: 05-08-2013, 05:54 PM
  4. Apply VBA code to filtered rows
    By jaff84 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-01-2012, 12:38 PM
  5. Chart apply to all filtered category
    By MitDac in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 12-02-2007, 09:59 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