Trying to get a macro to run when cell selection changes.

B

Bob Arnett

I have a workbook with two worksheets, each one with a table and I'm trying
to get a macro to run that filters the the second worksheet table with the
value of the the cell selected in column A of the first worksheet. After
getting some hints here on this forum I came up with:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Windows("Inventory Items.xlsm:1").Activate

If ActiveCell.Column = 1 Then
Dim ItemNum As String
ItemNum = "=" & ActiveCell.Text
Windows("Inventory Items.xlsm:2").Activate
ActiveSheet.ListObjects("Table_Default__ipurch").Range.AutoFilter
Field:=1, _
Criteria1:=ItemNum, Operator:=xlAnd
Windows("Inventory Items.xlsm:2").Activate
End If
End Sub

but it doesn't work automatically when I select another cell (in the first
column)even though it runs perfectly when I run it manually with the first
line changed to:

Sub FilterPurchases()

How does one get it to run automatically when the selection changes?
 
J

JLGWhiz

Did you put it in the worksheet code module? The worksheet_selctionchange
will not work from the standard code Module1. You must right click the name
tab of the active sheet, then click View Code from the drop down menu. That
opens the sheet code module. Paste your code there and then try it.
 
B

Bob Arnett

Excellent. I was just in the wrong place.

JLGWhiz said:
Did you put it in the worksheet code module? The worksheet_selctionchange
will not work from the standard code Module1. You must right click the name
tab of the active sheet, then click View Code from the drop down menu. That
opens the sheet code module. Paste your code there and then try it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top