macro disable all of events from work when press cancel

MKLAQ

Active Member
Joined
Jan 30, 2021
Messages
389
Office Version
  1. 2016
Platform
  1. Windows
hi

I search macro enable me to cancel any events from work on userform when I press button cancel

I have many codes in many events (textboxes,comboboxes ) when I press button(ok ) fill data in sheet , also I have some textboxes link with sheets when run userform it will show in sheet without using button (ok) will show in sheet . I want when I press button( cancel )return data sheet as was before run the userform . this should happens when press button (cancel) .
I no know if what I ask clear or possible does that .
if it's not clear just inform me to provide more details .
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Application.EnableEvents doesn't work on userform events. To control events on a userform, I use a module wide veariable DisableMyEvents and put the line If DisableMyEvents as the first line of every event sub. Then one can set the variable to True (and back to False) as needed.

VBA Code:
' in userform code module

Dim DisableMyEvents As Boolean

'...

Private Sub TextBox1_Change()
    If DisableMyEvents Then Exit Sub
    ' your code
End Sub

Private Sub ComboBox1_Click()
    If DisableMyEvents Then Exit Sub
    ' your code
End Sub
 
Upvote 0
thanks

Application.EnableEvents doesn't work on userform events

I want doing by command button(cancel) as I said .becuase I have code in UserForm_Initialize like autonumber of invoices in textbox link with cell value when run userform directly .

is it possible apply your code in command button(cancel) as i said . if yes .how should be , please?
 
Upvote 0
With the userform code set up as described, the code for the button might look something like

VBA Code:
Private Sub CommandButton1_Click()
    DisableMyEvents = Not(DisableMyEvents)
End Sub
 
Upvote 0
it doesn't work at all with UserForm_Initializ.
this is my code autonumbering every time when run userform . so the first time will populate in E2 trade/ 1000 based on textbox1 and when run the userform again should incermenting trade/ 1001 . based on your code should return the previous number trade/ 1000
this is the whole code
VBA Code:
Dim DisableMyEvents As Boolean



Private Sub CommandButton1_Click()
    DisableMyEvents = Not (DisableMyEvents)
End Sub


Private Sub UserForm_Initialize()
Dim ss As String
Dim r As Integer
ss = "trade"

If Range("E2") = "" Then 'Check if this is the first user of the form
    r = 1000
  Else
  r = Right(Range("e2").Value, 4) + 1 ' not, add 1 to last value
End If

Me.TextBox1.Text = ss & "/" & Str(r)
Range("E2").Value = Me.TextBox1.Text 'write this value to sheet(and later write other default values)

End Sub
 
Upvote 0
I'm not understanding your english. From some of the words used in the first posts, I've been guessing at what your issue is. But seeing that code and that issue shows I have no idea what problem you are trying to address.

Sorry.
 
Upvote 0
you see the code on userform directly show invoice of number in cell value E2 .when run userform from the first time based on textbox1 will move next invoice , but sometimes I decide cancel this number of invoice . for instance the invoice when run form from the first time .textbox1 show this number and linked with cell E2 . then it will show trade/ 1000 in cell E2 in this case if I press commandbutton(CANCEL) should keep number is trade/ 1000 ,and if I don't press commandbutton(cancel) then should incerement trade/ 1001 in cell E2 based on textbox1
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,835
Members
449,051
Latest member
excelquestion515

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