What is "Cancel=True"?

  • Thread starter Thread starter universal
  • Start date Start date
U

universal

Apologies if this is a simplistic question, but in order to help m
understanding of the macros that Ive been butchering and then addin
together, like a poor mans' Dr Frankenstein, I wondered what thi
meant.

Cancel = True (and indeed Cancel = False) statements are littere
across some macros that I am using, before and after functions, an
dialogue boxes.

I wonder if anyone could explain how this actually works and what i
refers to, and when it should be used?

Many thanks for everybodies continued understanding & patience,
E
 
Ed,

In this case, Cancel would be a Boolean (True or False) argument. It usually
pertains to event code and the Cancel argument allows the event to be
cancelled. So by setting Cancel to True, that event gets cancelled.

An example is this

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)

If Range("A1") = "" Then
MsgBox "Must enter value"
Cancel = True
End If

End Sub

This code is invoked if the workbook save button is invoked. It tests A1 for
data, and if there is none, displays a message, and sets the Cancel
argument. Net effect, the workbook deosn't get saved, as the event is
cancelled.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
1. Look up 'Cancel Property' in VB Editor help.
2. It is also used in worksheet/workbook event subroutines eg
BeforeSave to stop the process (in this case Save) occurring
 
Hi,
No idea - you will have to post an example macro. One possibility is
that a public variable called Cancel has been declared at the top of a
code module, so that it doesn't appear as a dim statement in the sub.
Another possibility is that the coder has not used Option Explicit at
the top of a module, so that this is a sub level variable. Yet another
possibility is that it says .Cancel = True (note the dot), and then
you proably have a Public variable declared in the code module behind
a Userform..
Impossible to say - post some code.

regards
Paul
 
Back
Top