How to stop a sub from executing

DAD

Board Regular
Joined
Jan 8, 2010
Messages
54
Hi All,

I have an increasingly complex workbook that has numerous subs to carry out specific tasks. One of the subs (lets call it "CellChange") checks to see if any of a range of cells on a worksheet is changed, and if any cells in that range are changed, it will execute 4 other separate subs to perform their task.

However, I now have a situation where I do not want "CellChange" to execute IF another sub is pasting data into the range of cells that would normally force "CellChange" to execute. Is there a way to do that?

Thanks in advance
 

Some videos you may like

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

MrIfOnly

Active Member
Joined
Nov 10, 2016
Messages
492
You could do something like this (note that the declaration of the boolean is outside any sub):

Code:
Dim cellchangecheck As Boolean

Sub change1()
    Sheets(1).Range("A1").Value = Sheets(1).Range("C1").Value
    cellchangecheck = True
End Sub


Sub CellChange()
    If cellchangecheck = True Then Exit Sub
    MsgBox "I am going to execute now"
End Sub

Regards,

CJ
 

lhartono

Board Regular
Joined
Nov 15, 2017
Messages
103
1. Add public boolean flag in public module so can be access from anywhere (maybe in Module1).
Code:
[COLOR=#0000cd]public booPaste as boolean[/COLOR]

2. in pasting sub, change the flag to true just before first pasting operation the change back to false afterwards.
Code:
[COLOR=#0000cd]sub Pasting

booPaste=true

[/COLOR][COLOR=#808080]..... pasting operations[/COLOR][COLOR=#0000cd]

booPaste=false

end sub[/COLOR]

3. in CellChange sub, add code to check the flag.
Code:
[COLOR=#0000cd]sub CellChange

if booPaste then 
     exit sub
end if

[/COLOR][COLOR=#808080].... cell change operations[/COLOR][COLOR=#0000cd]

end sub[/COLOR]
 

DAD

Board Regular
Joined
Jan 8, 2010
Messages
54
Hi guys,

Thanks for the tips. I will try both methods tonight and let you know how I go.

Cheers

Pete
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,276
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

If your CellChange is an event (such as Private Sub Worksheet_Change(ByVal Target As Range)) then you can disable them like this
Code:
Sub Macro1()

Application.EnableEvents = False

' Your code here

Application.EnableEvents = True

End Sub
 
  • Like
Reactions: DAD

DAD

Board Regular
Joined
Jan 8, 2010
Messages
54
Hi Fluff,

Brilliant, thank you for that. The CellChange Event was indeed a Private Sub. I entered the code as you suggested, and now it works a treat. Thank you.

Pete
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,276
Office Version
  1. 365
Platform
  1. Windows
Glad to help & thanks for the feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,127,057
Messages
5,622,449
Members
415,895
Latest member
Akhilesh28

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
Top