On worksheet change macro control

rjplante

Well-known Member
Joined
Oct 31, 2008
Messages
569
Office Version
  1. 365
Platform
  1. Windows
I have a macro (Macro A listed below) on a worksheet page VBA code window. Macro A will work in conjunction with conditional formatting and a defined Name which is a cell to capture the row I am on, to color the range of cells corresponding to the row I have selected. I also have a second macro (Macro B) in a module folder that turns on an autofilter and then filters based on a specific criteria. At the end of Macro A it turns on the protection of the sheet. Macro B unprotects the sheet at the beginning and then a range of data is selected. Because the range is selected MAcro A executes and then protects the sheet so that the rest of Macro B cannot run and I get an error that says I cannot turn on the autofilter with the page protected.

My question is this: How can I suspend the operation of Macro A during the execution of Macro B, and the rest of my macros for that matter?


Macro A in "Main Page" code window:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)


Sheets("Main Page").Unprotect


Range("L8:L5008").ClearContents


Range("A8:K5008").Locked = True


'Return active row or column in named cells.
[SelRow] = ActiveCell.Row


Sheets("Main Page").Protect


Call EDIT_TEXT


End Sub

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++


Sub EDIT_TEXT()


If ActiveCell.Row > 7 And ActiveCell.Column < 13 Then


    Range("L" & Range("AM2").Value).Value = "EDIT"


Else
    Exit Sub


End If


End Sub

Thanks for the help,

Robert
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
You're Edit_Text as shown will not call the SelectionChange event.
What you need to do is call the Edit_text code before you reprotect the sheet.
 
Upvote 0
I made that change, but it doesn't affect the issue I am facing. The code below is the one that will fail, along with others. It fails on the line below:

ActiveSheet.Range("$A$7:$K" & Lastrow).AutoFilter Field:=4, Criteria1:="Active"

Here is the code:
Code:
    Application.ScreenUpdating = False
    
    Sheets("Main Page").Unprotect
    
    Dim Lastrow As Long


'   Define last row of data
    Sheets("Main Page").Select
    Range("A100000").Select 'Row number is hopefully large enough to include all data
    Selection.End(xlUp).Select
    Lastrow = Selection.Row


'   if autofilter is on, show all the data
    Sheets("Main Page").Select
    If ActiveSheet.AutoFilterMode Then
        Range("A7:K7").Select
        Selection.AutoFilter
        Selection.AutoFilter
    End If
  
    ActiveSheet.Range("$A$7:$K" & Lastrow).AutoFilter Field:=4, Criteria1:="Active"


    Application.Goto Sheets("All Agreements").Range("A8"), True
    
    Sheets("Main Page").Protect
    
    Application.ScreenUpdating = True

I hope this helps.
 
Upvote 0
The easiest way round the problem is to add
Code:
Application.EnableEvents = False
to the start of the code &
Code:
Application.EnableEvents = True
to the end.
The better way would to be get rid of the .Select as you do not need to select anything.
Code:
   Application.ScreenUpdating = False
   Dim Lastrow As Long
   With Sheets("Main Page")
      .Unprotect
      '   Define last row of data
      Lastrow = Range("A" & Rows.Count).End(xlUp).Row
      '   if autofilter is on, show all the data
      If .AutoFilterMode Then .AutoFilterMode = False
   
      .Range("$A$7:$K" & Lastrow).AutoFilter Field:=4, Criteria1:="Active"
      Application.Goto Sheets("All Agreements").Range("A8"), True
   
      .Protect
   End With
   
   Application.ScreenUpdating = True
 
Upvote 0

Forum statistics

Threads
1,214,806
Messages
6,121,667
Members
449,045
Latest member
Marcus05

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