+ Reply to Thread
Results 1 to 15 of 15

Automatic macro trigger

Hybrid View

  1. #1
    Registered User
    Join Date
    01-29-2014
    Location
    St. Louis, MO
    MS-Off Ver
    Excel 2010
    Posts
    28

    Automatic macro trigger

    First time posting here and I'm relatively new to Excel in general. I created a macro that hides rows where every cell in that row lacks a numerical value. My macro is,

    Sub Hide()
    For Each Cell In Range("AB14:AB25")
    If Cell.Value = "Hide" Then
    Cell.EntireRow.Hidden = True
    Else
    Cell.EntireRow.Hidden = False
    End If
    Next Cell
    End Sub

    The range "AB14:AB25" refers to a column with an if statement that shoots out the value "hide" if each cell in that column is "N/A." The macro works but requires you to continuously and manually hit the macro button "Hide" whenever you change the relevant variable from the drop down menu (the table populates with data based on the variable you select from the drop down menu). So my question is, how do I make it so that the macro automatically triggers whenever you change or select a different variable from the drop down menu? Thanks in advance!

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Automatic macro trigger

    Put the code in the Worksheet_Change event:

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim c As Range
    For Each c In Range("AB14:AB25")
        If c.Value = "Hide" Then
            c.EntireRow.Hidden = True
        Else
            c.EntireRow.Hidden = False
        End If
    Next c
    End Sub
    Oh, and a couple of pointers:
    - Wrap CODE tags around code when you post
    - Cell is a poor variable name

    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Forum Expert
    Join Date
    11-28-2012
    Location
    Guatemala
    MS-Off Ver
    Excel 2010
    Posts
    2,394

    Re: Automatic macro trigger

    the automated macro can be triggered by ANY cell change or a selected range of cells

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    For Each Cell In Range("AB14:AB25")
    If Cell.Value = "Hide" Then
    Cell.EntireRow.Hidden = True
    Else
    Cell.EntireRow.Hidden = False
    End If
    Next Cell
    End Sub
    you can then restrict its triggering urge by testing target.row or target.column

  4. #4
    Registered User
    Join Date
    01-29-2014
    Location
    St. Louis, MO
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Automatic macro trigger

    Thank you for the help, and I'm sure that these macros work. The only issue I'm having now is that I don't know where to put these codes. I have 1 module with two codes:

    Sub Hide()
    For Each Cell In Range("AB14:AB25")
    If Cell.Value = "Hide" Then
    Cell.EntireRow.Hidden = True
    Else
    Cell.EntireRow.Hidden = False
    End If
    Next Cell
    End Sub

    Sub Show()
    Range("AB14:AB26").EntireRow.Hidden = False
    End Sub

    If I add the macros that you guys suggested to this module, and hit run, nothing happens. If I go to the worksheet titled "Universal table," hit "view code," copy the code onto there and run it, nothing happens either. Again, this is my first time working with VBA so I'm probably missing the obvious. In short, where do I put the codes that you kind gentleman have given me? Thanks again!

  5. #5
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Automatic macro trigger

    PLEASE put CODE tags around your posted code, as requested in the rules...

    Put my example code in the relevant worksheet module, and it will trigger whenever anything changes on that worksheet (to test, go to AB14, enter "Hide" and press enter.)

    See attachment for an example.
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    01-29-2014
    Location
    St. Louis, MO
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Automatic macro trigger

    I apologize for not including the code tags. Always forget to read the rules before I enter a new forum. Either way, it's still not working for me . Whenever I change a variable from the drop down menu, the rows with solely N/A values remain unless I manually trigger the "hide" macro. I know that I'm close, but can't get there... Thanks though.

  7. #7
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Automatic macro trigger

    Did you test by manually changing cell AB14 to "Hide" ?

  8. #8
    Registered User
    Join Date
    01-29-2014
    Location
    St. Louis, MO
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Automatic macro trigger

    Yeah, it will hide the row. My problem is that I need the macro to trigger whenever the value in cells Ab14:Ab25 change from "Hide" to "Show" based on variable changes from the drop down menu. In other words, I don't want to touch anything manually; I want the rows that lack numerical values to automatically hide whenever I change a variable from the dropdown menu. I want the rows that lack numerical values with one variable from the dropdown menu to reappear whenever I choose a variable (again, from the drop down menu) that populates that row with numerical values. I'm not sure If I'm being entirely clear..

  9. #9
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Automatic macro trigger

    Okay - so the code does what you want when values on the worksheet change - but not when "the drop down menu" changes.

    Now - what exactly do you mean by "the drop down menu", and what does it change in your workbook?

    Would probably be easier to help if you shared your workbook...

  10. #10
    Registered User
    Join Date
    01-29-2014
    Location
    St. Louis, MO
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Automatic macro trigger

    I'm not allowed to share my workbook because it contains client related data and personal information.

    So essentially, I have a 13x11 table where the columns are sorted by data source, region, source table, number of respondents, weight of consideration, standard deviation, 25th percentile, median, mean, 75th percentile and 90th percentile. The drop down menu allows you to populate this data based on region, specialty and metric. So for example, when you change the specialty from, say, Cardiologist to Rheumatologist, some of the rows that were populated with numerical values for the former no longer exist for the latter. Here's a specific example: there may not be any information for cardiologist legal fees in the Midwest region, but there may be information for rheumatologist legal fees in that region. I need that row to hide when I click on cardiologist, and then I need it to reemerge, automatically, when I change the specialty to rheumatology.

    I'm attempting to trigger my macro based on the if statements in column AB. When the row lacks numerical values, the if statement spits out "Hide," when it has values, it spits out "show." Thanks again. Hope this is somewhat clear.

  11. #11
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Automatic macro trigger

    No, it's not clear. WHAT IS the drop down menu? Is it data validation on a cell in the worksheet, is it a listbox /combo-box which calls some other code, is it an autofilter?

    Without understanding what this dropdown actually is, and what it controls, it's hard to understand how to pickup the right trigger to call your row hiding code.

  12. #12
    Registered User
    Join Date
    01-29-2014
    Location
    St. Louis, MO
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Automatic macro trigger

    It's a listbox tied to three pivot tables and to 3 cells which refer to the number corresponding to the individual values in each pivot table. The three cells are then combined to create a master reference cell from which all other cells draw their values via vlookup.

  13. #13
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Automatic macro trigger

    So trigger the Hide code from the Listbox_Change event.

    What type of listbox is it?

    If it's a Form Control, right click, and you'll see an option to "Assign Macro" - assign your 'Hide' macro.

    If it's an ActiveX Control, right click and you'll seen an option to "View Code" - do that, and enter the folllowing code (changing to the relevant listbox name):
    Private Sub Listbox1_Change(ByVal Target As Range)
    Dim c As Range
    For Each c In Range("AB14:AB25")
        If c.Value = "Hide" Then
            c.EntireRow.Hidden = True
        Else
            c.EntireRow.Hidden = False
        End If
    Next c
    End Sub

  14. #14
    Registered User
    Join Date
    01-29-2014
    Location
    St. Louis, MO
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Automatic macro trigger

    You are a hero. Thank you!

  15. #15
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Automatic macro trigger

    Glad we got there in the end

    Now, read my sig

+ 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. Macro to run on a trigger
    By randell.graybill in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-28-2009, 11:48 PM
  2. Prevent automatic combobox event trigger
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-09-2006, 04:00 PM
  3. macro trigger
    By Leslieac in forum Excel General
    Replies: 3
    Last Post: 02-02-2006, 05:10 PM
  4. Another way to trigger a macro?
    By Leon in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-22-2005, 02:10 AM
  5. Trigger Macro
    By OptionTrader in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 03-29-2005, 04:10 AM

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