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
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
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
 
Upvote 0
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]
 
Upvote 0
Hi guys,

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

Cheers

Pete
 
Upvote 0
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
Upvote 0
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
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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