ScreenUpdating false until explicitly enabled again - vba

I have a macro that calls a function after one second. To prevent flickering I want to disable ScreenUpdating. Unfortunately the property is reset to True after exiting test. Is there a way to disable the property until explicitly turned on again?
The code is roughly:
public sub test
Application.ScreenUpdating = False
App.OnTime Now + TimeValue("00:00:01"), "EnableScreenUpdating"
end sub
public sub EnableScreenupdating
Application.ScreenUpdating = True
end sub

Related

How to prevent any changes in code and be able at same time to look at it? (VBA)

Is there, by chance, any way to prevent from changing code and simultaneously be able to look at it?
The purpose is introductory, so that user could look at code without ability to do any changes.
Thank you in advance
Here is a tricky one. You can add the following three subscripts and it will make the file Read Only and also stop anyone from saving the workbook unless they use the SaveForReal Subscript/Macro.
Inside the ThisWorkbook VBA Object:
Private Sub Workbook_Open()
Application.DisplayAlerts = False
ActiveWorkbook.ChangeFileAccess Mode:=xlReadOnly
Application.DisplayAlerts = True
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
ThisWorkbook.Saved = True
Cancel = True
End Sub
Inside a Module Object:
Private Sub SaveForReal()
Application.EnableEvents = False
ThisWorkbook.Save
Application.EnableEvents = True
End Sub
To save the workbook, you need to open the VBA editor and Run the SaveForReal Subscript, otherwise the Save Button and Save As button does nothing.
Edit: Added On Open Read Only Change.

Using VBA code to modify OptionButton.value is activating the control.Click() Sub

I have a userform with 2 OptionButton choices, and I'm modifying the form (hiding labels and controls, and resizing frame) for the default Option (name = BwaIsNew), but then restoring the full userform when Option #2 (name = BwaIsOld) is selected. (see separate question for background).
When Option #2 is selected I'm calling a fresh userform, and coding the change in value. But this coding of the value dlgInformation.BwaIsOld.Value = True then triggers an event (?) that calls the Sub BwaIsOld_Click() code to run. This then sets up a perpetual loop.
What's the best way to solve this?
Problem code (the one looping) is:
Private Sub BwaIsOld_Click()
Unload Me
dlgInformation.BwaIsNew.Value = False
dlgInformation.BwaIsOld.Value = True
dlgInformation.Show
End Sub
Update:
Thanks #Tim & #CommonSense. I'm still not quite there yet. What am I doing wrong? Here is the code
Public EnableEvents As Boolean
Private Sub UserForm_Initialize()
Me.EnableEvents = True
End Sub
Private Sub BwaIsNew_Click()
Call changeform(280)
End Sub
Private Sub BwaIsOld_Click()
Unload Me
Me.EnableEvents = False
dlgInformation.BwaIsNew.Value = False
dlgInformation.BwaIsOld.Value = True
Me.EnableEvents = True
dlgInformation.Show
End Sub
You need to actually use that EnableEvents in the rest of your code.
BTW I would choose a different name from the built-in Application.EnableEvents property just for clarity.
Public EnableEvents As Boolean
Private Sub UserForm_Initialize()
Me.EnableEvents = True
End Sub
Private Sub BwaIsNew_Click()
'don't respond to events triggered by BwaIsOld_Click
If Me.EnableEvents Then
Call changeform(280)
End If
End Sub
Private Sub BwaIsOld_Click()
Unload Me '<< why do this here?
Me.EnableEvents = False
dlgInformation.BwaIsNew.Value = False
dlgInformation.BwaIsOld.Value = True
Me.EnableEvents = True
dlgInformation.Show
End Sub

Windows("ETY Tracker V1.2.xlsm").ScreenUpdating = False

Could you please help to solve the problem I am facing,when i run the program its throwing me an error "Object dosen't support this property or method"
Private Sub Workbook_Open()
'Application.Visible = False
'Application.ScreenUpdating = False
Windows("ETY Tracker V1.2.xlsm").ScreenUpdating = False
UserForm1.Show
End Sub
please help...I also tried both application .visible but it will close all the active workbooks we have, and application.screenupdating will not allow me to edit or open any other sheet other than the present user form.
Are you trying to hide the workbook and just show the form?
Private Sub Workbook_Open()
ThisWorkbook.Windows(1).Visible = False
UserForm1.Show
End Sub
Or hide Excel and show the form? This won't close the workbooks - they're just inside the application which in itself is hidden.
Private Sub Workbook_Open()
Application.Visible = False
UserForm1.Show
End Sub
Excel.Application.ScreenUpdating doesn't stop users from interacting with Excel, it just stops them from seeing the interaction.
Try Application.Interactive = False, but remember to set it back to true or you will have to close your excel application.

VBA EnableEvents fire textbox_change

I have a textbox with code on change, and if I press a button with the following code
Private Sub CommandButton1_Click()
Application.EnableEvents = False
TextBox1.Text = "new text"
Application.EnableEvents = True
End Sub
but this still fires the on change event of the textbox.
This happens because Application.EnableEvents allows enabling/disabling events fired from the Application (i.e. Excel).
In your case, the parent firing the TextBox1 change is not the Application but rather the UserForm. This is an example from cpearson about how to create your own EnableEvents property on your Userform. I report the content of the link here (to avoid "link-only answer"):
To suppress events in a form, you can create a variable at the form's
module level called "EnableEvents" and set that to False before
changing a property that will cause an event to be raised.
Public EnableEvents As Boolean
Private Sub UserForm_Initialize()
Me.EnableEvents = True
End Sub
Sub Something()
Me.EnableEvents = False
' some code that would cause an event to run
Me.EnableEvents = True
End Sub
Then, all of the controls on form should have a test if that variable
as their order of business in any event code. For example,
Private Sub ListBox1_Change()
If Me.EnableEvents = False Then
Exit Sub
End If
MsgBox "List Box Change"
End Sub
You can declare the EnableEvents as Private if only procedures with
that form need to suppress events. However, if you have forms that are
programmatically linked together, such UserForm2 adding an item to a
ListBox on UserForm1, you should declare the variable as Public and
set it for another form with code like the following:
UserForm1.EnableEvents = False
'
' change something on UserForm1
'
UserForm1.EnableEvents = True

Worksheet.RefreshAll triggers Worksheet_changed Event - how do I stop this from happeing?

I have an excel worksheet that is being used as a database front end for access. When a user changes data in a cell it will run the Worksheet_changed event to run codes that updates the access database.
However - I also have a refresh button that, you guessed it, refreshes the spreadsheet. This also causes the worksheet_changed event to run which will sometimes error out the program.
Private Sub RefreshButton_Click()
Refreshbuttons
ActiveWorkbook.RefreshAll
End Sub
How do I stop the work sheet changed event from happening when the refresh button is pressed? I have tried a Boolean flag which will stop the worksheet changed event from running when refreshed button is pressed - but it stops it from running at all (example of what I did)
Private Sub RefreshButton_Click()
Dim Flag as Boolean
Flag = True
Refreshbuttons
ActiveWorkbook.RefreshAll
Flag = False
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Flag = true then
Exit Sub
Else
[...] {Rest of code below here}
I am stuck - any help is greatly appreciated!!!
Thanks,
Ethan
EDIT
Thanks Tim! you pointed me in the right direction. I ended up going with (code below) and it worked beautifully. I appreciate everyones help!
Private Sub RefreshButton_Click()
For Each objConnection In ThisWorkbook.Connections
'Get current background-refresh value
bBackground = objConnection.OLEDBConnection.BackgroundQuery
'Temporarily disable background-refresh
objConnection.OLEDBConnection.BackgroundQuery = False
'Refresh this connection
objConnection.Refresh
If ActiveSheet.FilterMode = True Then
ActiveSheet.ShowAllData
Else
End If
'Set background-refresh value back to original value
objConnection.OLEDBConnection.BackgroundQuery = bBackground
Next
MsgBox "Refresh Complete"
End Sub
Private Sub RefreshButton_Click()
On Error Goto haveError
Refreshbuttons
Application.EnableEvents = False
ActiveWorkbook.RefreshAll
haveError:
Application.EnableEvents = True
End Sub
Declare Flag as global like below. It should work.
Public Flag as Boolean
All office applications have a built-in function to control this behavior. Simply add:
Application.EnableEvents = False to the beginning of the RefreshButton_Click event and
Application.EnableEvents = True to the end of the event.