Macro worksheet change

MarkReddell

Board Regular
Joined
Sep 1, 2011
Messages
210
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
I have several actions that need to be done within a worksheet change scenario. The Problem is that the whole macro runs instead of just the part that needs to work because of a cell change & it also goes into a loop or something. How do I run only the part of macro that needs to instead of ALL of macro? Here is what I'm trying to run:

Can anyone help me with this??? Thanx!!!

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address(False, False) = "A1" And Target.Value <> "" Then Call SORT_LIST
If Target.Address(False, False) = "A2" And Target.Value <> "" Then Call SORT_LIST
If Target.Address(False, False) = "A3" And Target.Value <> "" Then Call SORT_LIST
If Target.Address(False, False) = "A4" And Target.Value <> "" Then Call SORT_LIST
If Target.Address(False, False) = "A5" And Target.Value <> "" Then Call SORT_LIST
If Range("A17").Value = "YES" Then Call SHOW_TERMS_ONLY
If Range("A17").Value = "NO" Then Call DONT_SHOW_TERMS_ONLY
If Target.Address <> "$B$2" Then Exit Sub
Application.Run Target.Value
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
This might bring you closer to what you want, but will need testing and modifying surely:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Application.Intersect(Target, Range("A1:A5")) Is Nothing Then
        If Target.Value <> "" Then Call SORT_LIST
    End If
    
    If Range("A17").Value = "YES" Then
        Call SHOW_TERMS_ONLY
    ElseIf Range("A17").Value = "NO" Then
        Call DONT_SHOW_TERMS_ONLY
    End If
    
    If Target.Address = "$B$2" Then Application.Run Target.Value

End Sub
 
Upvote 0
Thanx 4 your reply & help!!! It works expect this part of macro! When I run a macro command from B2, it doesn't run those without causing debug problem that doesn't happen if I don't use it within the worksheet change!!! I think the problem is that the code below tries to run also!


If Target.Address = "$B$2" Then Application.Run Target.Value
 
Upvote 0
Not sure I understood your point, however in addition to wigi suggestion I recommend to insert Application.EnableEvents = False at the beginning of the macro and Application.EnableEvents = True before exiting; for example:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False    ' ADD
If Target.Address(False, False) = "A1" And Target.Value <> "" Then Call SORT_LIST
If Target.Address(False, False) = "A2" And Target.Value <> "" Then Call SORT_LIST
If Target.Address(False, False) = "A3" And Target.Value <> "" Then Call SORT_LIST
If Target.Address(False, False) = "A4" And Target.Value <> "" Then Call SORT_LIST
If Target.Address(False, False) = "A5" And Target.Value <> "" Then Call SORT_LIST
If Range("A17").Value = "YES" Then Call SHOW_TERMS_ONLY
If Range("A17").Value = "NO" Then Call DONT_SHOW_TERMS_ONLY
If Target.Address = "$B$2" Then Application.Run Target.Value  'MODIFIED
Application.EnableEvents = True    'ADD
End Sub
This should prevent the macro from looping in case the called macro itsel modify the sheet, as I suspect.

Bye
 
Upvote 0
oops!!! How do I fix the problem with the macro "Sort_List" from running ever time a make a worksheet change in other unrelated cells???
 
Upvote 0
This requires following wigi suggestion:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
CheckArea="A1:A5"     'The area to be monitored PUT YOUR VALUE
If Application.Intersect(Target, Range(CheckArea)) Is Nothing Then Exit Sub
Application.EnableEvents = False    ' ADD
If Target.Address(False, False) = "A1" And Target.Value <> "" Then Call SORT_LIST
'etc etc
Bye
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,263
Members
452,627
Latest member
KitkatToby

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