problem with execute macro when change value of specific cell

maabadi

Well-known Member
Joined
Oct 22, 2012
Messages
2,681
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hi
I record this macro for auto refresh filter of table based of value of cell "B2"
Code:
Sub Mysub()
'
' RefreshFilters Macro
'

'
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=2
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=2, Criteria1:= _
        "<>"
End Sub

And Before that I define this Worksheet_Change macro command
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Call RefreshFilters(Me.Name, Target.Address, "$B$2")
End Sub

But it didn't work and didn't execute macro when cell "B2" value is changed by hand.

what is problem
thanks
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Events in VBA can be sensitive. Did you write
Code:
[COLOR=#333333]Private Sub Worksheet_Change(ByVal Target As Range)
by yourself or use the dropdown menu?

If you didnt use the dropdown menu you may find it doesnt work[/COLOR]
 
Upvote 0
Hi,
As an observation, your Worksheet Change event is calling the procedure RefreshFilters which has three parameters.

The Code you show here is named MySub with no parameters? So you change event will not be call this macro. If the code is not erroring when cell change is made then clearly the RefreshFilters code exists & would be helpful to board if you share that code.

Can I hazard a guess that following may be something like what you are trying to do:

In a Standard module:
Code:
Sub RefreshFilters(ByVal Target As Range)
'
' RefreshFilters Macro
'
'
    With Target.Parent.ListObjects("Table1")
        .Range.AutoFilter Field:=Target.Column
        .Range.AutoFilter Field:=Target.Column, Criteria1:="<>"
    End With
End Sub

In your Worksheest Code Page:

Code:
 Private Sub Worksheet_Change(ByVal Target As Range)    
   If Target.Address = "$B$2" Then Call RefreshFilters(Target)
End Sub


Dave
 
Last edited:
Upvote 0
thanks dmt32
you're right.
It worked after I right click on sheet name and view code and after that import codes.
 
Upvote 0

Forum statistics

Threads
1,216,218
Messages
6,129,571
Members
449,518
Latest member
srooney

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top