excel - Get Worksheet_change event to execute macro automatically -
i have macro (applyfilter) filters through many worksheets based on date enter cell b1 on worksheet (grand totals). macro is:
sub applyfilter() 'filters worksheets except worksheet1 date entered _ 'grand totals!b1 dim ws_count integer dim integer dim filterrange variant filterrange = range("'grand totals'!b1") ' set ws_count equal number of worksheets in active ' workbook. ws_count = activeworkbook.worksheets.count ' begin loop. = 2 ws_count sheets(i).select activesheet.autofiltermode = false 'remove existing filters worksheets(i).range("a2").autofilter field:=1, criteria1:=range("'grand totals'!b1").text next sheet1.activate end sub
when execute macro manually, executes , filters should. however, when call macro sub:
sub worksheet_change(byval target range) if not intersect(target, me.range("b1")) nothing _ call applyfilter end sub
i "macros" window offers list of macros available. can select "applyfilter" macro , click run , macro executes , filters worksheets desire.
i found many references automatically executing macro within sub, none refer "macros" window, must select macro run. instead, when enter date in cell b1 of "grand totals" worksheet , hit enter, sub worksheet_change(byval target range) should automatically call "applyfilter" , apply date filter many worksheets.
i have created button , used button_click call "applyfilter", , well. but, seems more intuitive enter date , press enter execute macro. put button_click method, i'm trying learn vba firstly, , i'm stubborn enough want learn how make work, , not want settle work.
sheet code must in grand totals
sheet
grand totals sheet code
sub worksheet_change(byval target range) if not intersect(target, me.range("b1")) nothing call applyfilter end sub
more efficient filter code
sub applyfilter() dim ws worksheet each ws in activeworkbook.sheets if ws.name <> "grand totals" ws.autofiltermode = false ws.range("a2").autofilter field:=1, criteria1:=range("'grand totals'!b1").text end if next end sub
Comments
Post a Comment