Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default Capture a cancel button

Hi all,
I record a macro that unprotect a sheet and attached it to a form button.
When i click the button the unprotect sheet (coming from the application)
form appears. the problem is that if i click the 'Cancel' button the rest of
my code is still executed.(this i want prevent). how can i trap the cancel
event and prevent it from executing the rest of the code?
Thanks in advance
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Capture a cancel button

Hi Lp_12,

how can i trap the cancel event


You could use the userform's QueryClose event. For example:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
' Cancel button pressed, do something. e.g.:
MsgBox "You pressed the red X!"
Else
'do something else, e.g.:
MsgBox "You closed the form without using the red X"
End If
End Sub


---
Regards,
Norman



"Lp12" wrote in message
...
Hi all,
I record a macro that unprotect a sheet and attached it to a form button.
When i click the button the unprotect sheet (coming from the application)
form appears. the problem is that if i click the 'Cancel' button the rest
of
my code is still executed.(this i want prevent). how can i trap the cancel
event and prevent it from executing the rest of the code?
Thanks in advance



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default Capture a cancel button

Hi Norman,
Thanks for the reply.
I wanted to know how does my code knows that the evet started?
Do i hae to copy it to my custom sub or to call it to another funkction?
Thanks again

"Norman Jones" wrote:

Hi Lp_12,

how can i trap the cancel event


You could use the userform's QueryClose event. For example:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
' Cancel button pressed, do something. e.g.:
MsgBox "You pressed the red X!"
Else
'do something else, e.g.:
MsgBox "You closed the form without using the red X"
End If
End Sub


---
Regards,
Norman



"Lp12" wrote in message
...
Hi all,
I record a macro that unprotect a sheet and attached it to a form button.
When i click the button the unprotect sheet (coming from the application)
form appears. the problem is that if i click the 'Cancel' button the rest
of
my code is still executed.(this i want prevent). how can i trap the cancel
event and prevent it from executing the rest of the code?
Thanks in advance




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Capture a cancel button

Hi Lp_12,

I wanted to know how does my code knows that the evet started?


The UserForm_QueryClose event is called whenever the form is closed.

Try adding the suggested demo code to the userform module. Then try closing
your form, firstly by using the X close icon and, secondly, by your
programmatic means - via a commandbutton, perhaps.


---
Regards,
Norman



"Lp12" wrote in message
...
Hi Norman,
Thanks for the reply.
I wanted to know how does my code knows that the evet started?
Do i hae to copy it to my custom sub or to call it to another funkction?
Thanks again

"Norman Jones" wrote:

Hi Lp_12,

how can i trap the cancel event


You could use the userform's QueryClose event. For example:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
' Cancel button pressed, do something. e.g.:
MsgBox "You pressed the red X!"
Else
'do something else, e.g.:
MsgBox "You closed the form without using the red X"
End If
End Sub


---
Regards,
Norman



"Lp12" wrote in message
...
Hi all,
I record a macro that unprotect a sheet and attached it to a form
button.
When i click the button the unprotect sheet (coming from the
application)
form appears. the problem is that if i click the 'Cancel' button the
rest
of
my code is still executed.(this i want prevent). how can i trap the
cancel
event and prevent it from executing the rest of the code?
Thanks in advance






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default Capture a cancel button

Hi Norman,
I want to clarify that i didn't create any custom userform rather i'm using
the:
ActiveSheet.UnProtect which opens up the excel own "Unprotect" form asking
for the password.
Do i make any sense?
Thanks for the patience

"Norman Jones" wrote:

Hi Lp_12,

I wanted to know how does my code knows that the evet started?


The UserForm_QueryClose event is called whenever the form is closed.

Try adding the suggested demo code to the userform module. Then try closing
your form, firstly by using the X close icon and, secondly, by your
programmatic means - via a commandbutton, perhaps.


---
Regards,
Norman



"Lp12" wrote in message
...
Hi Norman,
Thanks for the reply.
I wanted to know how does my code knows that the evet started?
Do i hae to copy it to my custom sub or to call it to another funkction?
Thanks again

"Norman Jones" wrote:

Hi Lp_12,

how can i trap the cancel event

You could use the userform's QueryClose event. For example:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
' Cancel button pressed, do something. e.g.:
MsgBox "You pressed the red X!"
Else
'do something else, e.g.:
MsgBox "You closed the form without using the red X"
End If
End Sub


---
Regards,
Norman



"Lp12" wrote in message
...
Hi all,
I record a macro that unprotect a sheet and attached it to a form
button.
When i click the button the unprotect sheet (coming from the
application)
form appears. the problem is that if i click the 'Cancel' button the
rest
of
my code is still executed.(this i want prevent). how can i trap the
cancel
event and prevent it from executing the rest of the code?
Thanks in advance








  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Capture a cancel button

Hi Lp_12,

I want to clarify that i didn't create any custom userform


Sorry - I misunderstood!

Please post the code that you have assigned to the button

---
Regards,
Norman



"Lp12" wrote in message
...
Hi Norman,
I want to clarify that i didn't create any custom userform rather i'm
using
the:
ActiveSheet.UnProtect which opens up the excel own "Unprotect" form asking
for the password.
Do i make any sense?
Thanks for the patience

"Norman Jones" wrote:

Hi Lp_12,

I wanted to know how does my code knows that the evet started?


The UserForm_QueryClose event is called whenever the form is closed.

Try adding the suggested demo code to the userform module. Then try
closing
your form, firstly by using the X close icon and, secondly, by your
programmatic means - via a commandbutton, perhaps.


---
Regards,
Norman



"Lp12" wrote in message
...
Hi Norman,
Thanks for the reply.
I wanted to know how does my code knows that the evet started?
Do i hae to copy it to my custom sub or to call it to another
funkction?
Thanks again

"Norman Jones" wrote:

Hi Lp_12,

how can i trap the cancel event

You could use the userform's QueryClose event. For example:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As
Integer)
If CloseMode = vbFormControlMenu Then
' Cancel button pressed, do something. e.g.:
MsgBox "You pressed the red X!"
Else
'do something else, e.g.:
MsgBox "You closed the form without using the red X"
End If
End Sub


---
Regards,
Norman



"Lp12" wrote in message
...
Hi all,
I record a macro that unprotect a sheet and attached it to a form
button.
When i click the button the unprotect sheet (coming from the
application)
form appears. the problem is that if i click the 'Cancel' button the
rest
of
my code is still executed.(this i want prevent). how can i trap the
cancel
event and prevent it from executing the rest of the code?
Thanks in advance








  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default Capture a cancel button

Here you go Norman,


Sub UnProtect()

On Error Resume Next
Application.ScreenUpdating = False

' Setting the users last cursor position
Currrange = ActiveCell.Address
' Checking for the Verify column
Range("A3").Select
i = 1
Do
If ActiveCell.Value < "Verify By" Then
ActiveCell.Offset(0, 1).Select
i = i + 1
End If
Loop Until ActiveCell.Value = "Verify By"
Vercol = ActiveCell.Column
mycol = ActiveCell.Column
Range(Currrange).Select
If ActiveCell.Column < mycol Then
MsgBox "Please set the cursor in the row you want to verify"
Exit Sub
End If
ActiveSheet.UnProtect
ActiveCell.Value = Application.UserName
ActiveCell.Offset(0, 1).Value = Now
Do
ActiveCell.Offset(0, -1).Locked = True
ActiveCell.Offset(0, -1).Select
mycol = mycol - 1
Loop Until mycol = 1
ActiveSheet.Protect Password:="3455" _
, DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingCells:=True, AllowInsertingHyperlinks:=True
End Sub

thanks

"Norman Jones" wrote:

Hi Lp_12,

I want to clarify that i didn't create any custom userform


Sorry - I misunderstood!

Please post the code that you have assigned to the button

---
Regards,
Norman



"Lp12" wrote in message
...
Hi Norman,
I want to clarify that i didn't create any custom userform rather i'm
using
the:
ActiveSheet.UnProtect which opens up the excel own "Unprotect" form asking
for the password.
Do i make any sense?
Thanks for the patience

"Norman Jones" wrote:

Hi Lp_12,

I wanted to know how does my code knows that the evet started?

The UserForm_QueryClose event is called whenever the form is closed.

Try adding the suggested demo code to the userform module. Then try
closing
your form, firstly by using the X close icon and, secondly, by your
programmatic means - via a commandbutton, perhaps.


---
Regards,
Norman



"Lp12" wrote in message
...
Hi Norman,
Thanks for the reply.
I wanted to know how does my code knows that the evet started?
Do i hae to copy it to my custom sub or to call it to another
funkction?
Thanks again

"Norman Jones" wrote:

Hi Lp_12,

how can i trap the cancel event

You could use the userform's QueryClose event. For example:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As
Integer)
If CloseMode = vbFormControlMenu Then
' Cancel button pressed, do something. e.g.:
MsgBox "You pressed the red X!"
Else
'do something else, e.g.:
MsgBox "You closed the form without using the red X"
End If
End Sub


---
Regards,
Norman



"Lp12" wrote in message
...
Hi all,
I record a macro that unprotect a sheet and attached it to a form
button.
When i click the button the unprotect sheet (coming from the
application)
form appears. the problem is that if i click the 'Cancel' button the
rest
of
my code is still executed.(this i want prevent). how can i trap the
cancel
event and prevent it from executing the rest of the code?
Thanks in advance









  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Capture a cancel button

Hi Lp_12,

Try something like:

'===========================
Public Sub UnProtect()
Dim Currrange As Range
Dim i As Long
Dim Vercol As Long
Dim mycol As Long

On Error Resume Next
Application.ScreenUpdating = False

' Setting the users last cursor position
Currrange = ActiveCell.Address
' Checking for the Verify column
Range("A3").Select
i = 1
Do
If ActiveCell.Value < "Verify By" Then
ActiveCell.Offset(0, 1).Select
i = i + 1
End If
Loop Until ActiveCell.Value = "Verify By"
Vercol = ActiveCell.Column
mycol = ActiveCell.Column
Range(Currrange).Select
If ActiveCell.Column < mycol Then
MsgBox "Please set the cursor in the row you want to verify"
Exit Sub
End If
ActiveSheet.UnProtect
If Not IsProtected(ActiveSheet) Then
ActiveCell.Value = Application.UserName
ActiveCell.Offset(0, 1).Value = Now
Do
ActiveCell.Offset(0, -1).Locked = True
ActiveCell.Offset(0, -1).Select
mycol = mycol - 1
Loop Until mycol = 1
ActiveSheet.Protect Password:="3455", _
DrawingObjects:=True, _
Contents:=True, _
Scenarios:=True, _
AllowFormattingCells:=True, _
AllowInsertingHyperlinks:=True
Else
MsgBox ActiveSheet.Name & " has not been unprotected!"
End If
End Sub

'--------------------------------------------

Function IsProtected(Optional WS As Worksheet) As Boolean
If WS Is Nothing Then Set WS = ActiveSheet
If WS.ProtectScenarios Or _
WS.ProtectDrawingObjects Or _
WS.ProtectContents Then
IsProtected = True
End If

End Function
'<<===========================


Apart from using a call to the additional IsProtected function, I have
explicitly dimmed your variables, which is good practice.

I have not otherwise attempted to understand your procedures logic.


---
Regards,
Norman



"Lp12" wrote in message
...
Here you go Norman,


Sub UnProtect()

On Error Resume Next
Application.ScreenUpdating = False

' Setting the users last cursor position
Currrange = ActiveCell.Address
' Checking for the Verify column
Range("A3").Select
i = 1
Do
If ActiveCell.Value < "Verify By" Then
ActiveCell.Offset(0, 1).Select
i = i + 1
End If
Loop Until ActiveCell.Value = "Verify By"
Vercol = ActiveCell.Column
mycol = ActiveCell.Column
Range(Currrange).Select
If ActiveCell.Column < mycol Then
MsgBox "Please set the cursor in the row you want to verify"
Exit Sub
End If
ActiveSheet.UnProtect
ActiveCell.Value = Application.UserName
ActiveCell.Offset(0, 1).Value = Now
Do
ActiveCell.Offset(0, -1).Locked = True
ActiveCell.Offset(0, -1).Select
mycol = mycol - 1
Loop Until mycol = 1
ActiveSheet.Protect Password:="3455" _
, DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingCells:=True, AllowInsertingHyperlinks:=True
End Sub

thanks

"Norman Jones" wrote:

Hi Lp_12,

I want to clarify that i didn't create any custom userform


Sorry - I misunderstood!

Please post the code that you have assigned to the button

---
Regards,
Norman



"Lp12" wrote in message
...
Hi Norman,
I want to clarify that i didn't create any custom userform rather i'm
using
the:
ActiveSheet.UnProtect which opens up the excel own "Unprotect" form
asking
for the password.
Do i make any sense?
Thanks for the patience

"Norman Jones" wrote:

Hi Lp_12,

I wanted to know how does my code knows that the evet started?

The UserForm_QueryClose event is called whenever the form is closed.

Try adding the suggested demo code to the userform module. Then try
closing
your form, firstly by using the X close icon and, secondly, by your
programmatic means - via a commandbutton, perhaps.


---
Regards,
Norman



"Lp12" wrote in message
...
Hi Norman,
Thanks for the reply.
I wanted to know how does my code knows that the evet started?
Do i hae to copy it to my custom sub or to call it to another
funkction?
Thanks again

"Norman Jones" wrote:

Hi Lp_12,

how can i trap the cancel event

You could use the userform's QueryClose event. For example:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As
Integer)
If CloseMode = vbFormControlMenu Then
' Cancel button pressed, do something. e.g.:
MsgBox "You pressed the red X!"
Else
'do something else, e.g.:
MsgBox "You closed the form without using the red X"
End If
End Sub


---
Regards,
Norman



"Lp12" wrote in message
...
Hi all,
I record a macro that unprotect a sheet and attached it to a form
button.
When i click the button the unprotect sheet (coming from the
application)
form appears. the problem is that if i click the 'Cancel' button
the
rest
of
my code is still executed.(this i want prevent). how can i trap
the
cancel
event and prevent it from executing the rest of the code?
Thanks in advance











  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default Capture a cancel button

Norman,
Thanks a lot for all the help.
Its working gr8...
Peace

"Norman Jones" wrote:

Hi Lp_12,

Try something like:

'===========================
Public Sub UnProtect()
Dim Currrange As Range
Dim i As Long
Dim Vercol As Long
Dim mycol As Long

On Error Resume Next
Application.ScreenUpdating = False

' Setting the users last cursor position
Currrange = ActiveCell.Address
' Checking for the Verify column
Range("A3").Select
i = 1
Do
If ActiveCell.Value < "Verify By" Then
ActiveCell.Offset(0, 1).Select
i = i + 1
End If
Loop Until ActiveCell.Value = "Verify By"
Vercol = ActiveCell.Column
mycol = ActiveCell.Column
Range(Currrange).Select
If ActiveCell.Column < mycol Then
MsgBox "Please set the cursor in the row you want to verify"
Exit Sub
End If
ActiveSheet.UnProtect
If Not IsProtected(ActiveSheet) Then
ActiveCell.Value = Application.UserName
ActiveCell.Offset(0, 1).Value = Now
Do
ActiveCell.Offset(0, -1).Locked = True
ActiveCell.Offset(0, -1).Select
mycol = mycol - 1
Loop Until mycol = 1
ActiveSheet.Protect Password:="3455", _
DrawingObjects:=True, _
Contents:=True, _
Scenarios:=True, _
AllowFormattingCells:=True, _
AllowInsertingHyperlinks:=True
Else
MsgBox ActiveSheet.Name & " has not been unprotected!"
End If
End Sub

'--------------------------------------------

Function IsProtected(Optional WS As Worksheet) As Boolean
If WS Is Nothing Then Set WS = ActiveSheet
If WS.ProtectScenarios Or _
WS.ProtectDrawingObjects Or _
WS.ProtectContents Then
IsProtected = True
End If

End Function
'<<===========================


Apart from using a call to the additional IsProtected function, I have
explicitly dimmed your variables, which is good practice.

I have not otherwise attempted to understand your procedures logic.


---
Regards,
Norman



"Lp12" wrote in message
...
Here you go Norman,


Sub UnProtect()

On Error Resume Next
Application.ScreenUpdating = False

' Setting the users last cursor position
Currrange = ActiveCell.Address
' Checking for the Verify column
Range("A3").Select
i = 1
Do
If ActiveCell.Value < "Verify By" Then
ActiveCell.Offset(0, 1).Select
i = i + 1
End If
Loop Until ActiveCell.Value = "Verify By"
Vercol = ActiveCell.Column
mycol = ActiveCell.Column
Range(Currrange).Select
If ActiveCell.Column < mycol Then
MsgBox "Please set the cursor in the row you want to verify"
Exit Sub
End If
ActiveSheet.UnProtect
ActiveCell.Value = Application.UserName
ActiveCell.Offset(0, 1).Value = Now
Do
ActiveCell.Offset(0, -1).Locked = True
ActiveCell.Offset(0, -1).Select
mycol = mycol - 1
Loop Until mycol = 1
ActiveSheet.Protect Password:="3455" _
, DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingCells:=True, AllowInsertingHyperlinks:=True
End Sub

thanks

"Norman Jones" wrote:

Hi Lp_12,

I want to clarify that i didn't create any custom userform

Sorry - I misunderstood!

Please post the code that you have assigned to the button

---
Regards,
Norman



"Lp12" wrote in message
...
Hi Norman,
I want to clarify that i didn't create any custom userform rather i'm
using
the:
ActiveSheet.UnProtect which opens up the excel own "Unprotect" form
asking
for the password.
Do i make any sense?
Thanks for the patience

"Norman Jones" wrote:

Hi Lp_12,

I wanted to know how does my code knows that the evet started?

The UserForm_QueryClose event is called whenever the form is closed.

Try adding the suggested demo code to the userform module. Then try
closing
your form, firstly by using the X close icon and, secondly, by your
programmatic means - via a commandbutton, perhaps.


---
Regards,
Norman



"Lp12" wrote in message
...
Hi Norman,
Thanks for the reply.
I wanted to know how does my code knows that the evet started?
Do i hae to copy it to my custom sub or to call it to another
funkction?
Thanks again

"Norman Jones" wrote:

Hi Lp_12,

how can i trap the cancel event

You could use the userform's QueryClose event. For example:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As
Integer)
If CloseMode = vbFormControlMenu Then
' Cancel button pressed, do something. e.g.:
MsgBox "You pressed the red X!"
Else
'do something else, e.g.:
MsgBox "You closed the form without using the red X"
End If
End Sub


---
Regards,
Norman



"Lp12" wrote in message
...
Hi all,
I record a macro that unprotect a sheet and attached it to a form
button.
When i click the button the unprotect sheet (coming from the
application)
form appears. the problem is that if i click the 'Cancel' button
the
rest
of
my code is still executed.(this i want prevent). how can i trap
the
cancel
event and prevent it from executing the rest of the code?
Thanks in advance












Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding a cancel button Amir Excel Programming 4 September 3rd 05 01:40 AM
Cancel Button on Userform KJ Dahl Excel Programming 2 December 19th 04 07:47 PM
InputBox - cancel button Greg Billinge Excel Programming 4 October 11th 04 06:45 PM
Cancel button IamHaxer Excel Programming 3 June 24th 04 05:19 PM
Cancel Button Matt Excel Programming 3 February 3rd 04 11:23 PM


All times are GMT +1. The time now is 03:49 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"