What's the lifetime of Application.ScreenUpdating value in Excel 2010? - excel

As far as I know Application.ScreenUpdating = true value is maintained until Application.ScreenUpdating = false is set. But what's the 'lifetime' of this value? Is it while the procedure where it's called starts and finish, while Worksheet is opened, or?
Made a class to keep ScreenUpdating value consistent to my needs:
Init class
set ScreenUpdating = False
At the end of procedure or in case of error restore the value to True.
The class handles this case ok.
Some times need the opposite:
Init class
set ScreenUpdating = True
At the end of procedure or in case of error restore the value to False.
I'm having trouble here; the class sets properly the value ScreenUpdating = false, but when the class gets the 'actual' value of ScreenUpdating it is always true. There are no other procedures or addins that could be changing the value.
Have prepared a test sheet to show the points above. Select the value for ScreenUpdating from the dropdown above the "suCaller" button.
Select False and press the button.
Value before setting False is True as expected.
Test data is filled from another procedure showing ScreenUpdating new value (False).
After data fill, the value is reset to True.
Press the button again and 'Actual' value is True as expected.
Do it many times and values should be fine.
Now Select True and press the button.
Value before setting True is True as expected (which is the value from above)
Test data is filled again showing ScreenUpdating new value (True)
After data fill, the value is reset to False.
Press the button again and 'Actual' value is True not False.
The class just negate the new value to define the restore value, so the result confuses me on the lifetime or how ScreenUpdating value should be set.
Am I doing something wrong in the class or missing some basic theory?
Class Module: ApplicationScreenUpdate
Test Module: Test
Test Sheet
TIA, Oscar.

So far as I'm aware this is behaviour is by design (but I don't have a quotable source for that).
While an experienced developer would expect the setting to remain, an inexperienced one would probably not realise what they'd done and would think that excel was broken if they tried to manually put data into the sheet after their macro completed.
Something you could try is to disable the application events as sometimes they can reset the value, also you may want to try writing a value after you set the updating to false but I suspect that value will appear after the macro is done.

Have found that the lifetime for ScreenUpdating or DisplayAlerts value, is the top procedure where the value was modified. At the end of the procedure; even if the value was set to false, the value for this properties ALWAYS is restored to TRUE.
On the other hand, EnableEvents or Calculation retains its value at the end of the procedure where the value was modified, been TRUE or FALSE.
Open this workbook and run the methods in the following order:
TestFalse
TestCurrentValues
TestValues
TestCurrentValues
RestoreValues
TestCurrentValues
After running each method, see values in sheet.
Cannot confirm wich application properties modify the value of other ones. But as James wrote, some change its own value 'by design'

Related

code runs differently with/without breakpoint

I'm creating a user form (with SAVE button that writes to Excel sheet and RETURN button that goes back to a splash page) and want to set/check a flag if there are unsaved changes on the form. I'm getting different functionality of the code depending on whether a breakpoint is set or not.
Here is the relevant portion of code:
Public saveFlag as Boolean
Private Sub UserForm_Initialize()
initializeCMAValues Me
initializeTeamValues Me, getRowNum("CMA", "Team", False)
' getRowNum is a function that returns the row to read from
' in this case, the row with name=CMA, type=Team, createRow=False
saveFlag = False
End Sub
Private Sub btn_Return_Click()
' Check for unsaved data
If saveFlag Then
If MsgBox("You have unsaved information on the form. Do you wish to save it?", vbYesNo, "Unsaved Data") = vbYes Then
btn_Save_Click
End If
End If
' Return to splash form
Unload Me
Form_Splash.Show
End Sub
If I enter the form and select RETURN, I get the msgbox prompt. If I set a breakpoint at the saveFlag = False line, enter form, select RETURN (invoking breakpoint), continue running, then it runs as I would expect (i.e., no msgbox).
Instead of a public saveFlag variable, I've tried writing the flag to a non-visible label on the form; I've also tried writing to a cell on the Excel sheet. Both gave the same results of this different behavior with and without the breakpoint.
I've also run the code with the initializeTeamValues line commented out and then the code runs fine. I've triple checked that this subroutine only fills values into the textboxes on the form, nothing else.
Some research turned up a vague answer related to OLE pushing values onto the stack. I then tried putting the "saveFlag = False" line at the beginning of the subroutine. Same results.
Ideas of why the varying behavior? Is there a better/alternate way to check for unsaved data?

Can two checkboxes be linked in a way that only one will ever be checked?

I am working on moving a form over to Excel. Due to the style of form I have to replicate the form with no changes.
On the form there are two check boxes, one for Full and one for partial.
I have the code spit out a warning box if both are selected. I think it would be easier to make it so that if one is checked the other will automatically uncheck.
' ERROR CHECK FULL/PARTIAL
If [F8] = True And [F9] = True Then
MsgBox ("FAI can not be both a Full and Partial")
ELSE
'actual code in middle.
END IF
There are two ways to do this. With Radio buttons, or checking if the other box is checked.
With radio buttons is simpler if one of them must be selected no matter what. You can enclose radio buttons on different frames to "group" them together as in the screenshot below. You can see that one on each frame is selected and you can only select one of the two on each frame.
If you really want to use a Checkbox, you can use the code below.
Private Sub CheckBox1_Click()
If Me.CheckBox1 = True Then
If Me.CheckBox2 = True Then Me.CheckBox2 = False
End If
End Sub
Private Sub CheckBox2_Click()
If Me.CheckBox2 = True Then
If Me.CheckBox1 = True Then Me.CheckBox1 = False
End If
End Sub
The first If checks if this is a click to Check the box, if it is, then it will check if the second box is already checked, if it is, it will un-check it.
This is assuming you are creating a UserForm, and these are not CheckBoxes on an Excel Sheet. The implementation would be the same, but the code will be slightly different.

Powerapps Visible function

For some reason the Visible function in my Powerapps won't work I just wrote in OnSelect() Mail.Visible = false
The Mail is in this case a Textinput/ TextBox.
When I click on the button nothing happens. I can't find a documentation about it on the MS Website but I have in Powerapps a fuction called "Visible"
You need to create a variable in the button's (or another control) OnSelect property:
UpdateContext({ mailVisible: false })
And set the Visible property of the Mail control to mailVisible. You may need to initialize that variable to true, for example, in the screen's OnVisible property:
UpdateContext({ mailVisible: true })
PowerApps works similarly to Excel - you cannot, by an action, change directly the value of a cell (e.g., A1 = 42). But you can make the A1 cell reference another cell (say, =A4), so when you change the value of the cell A4, A1 will be updated as well. The same principle applies in PowerApps - you cannot change the value of a property from an action, but you can update the value that the property references.
Credit #SeaDude
This worked perfectly for me toggling the variable back and forth to show/hide a few layers.
Set(mailVisible, !mailVisible)
So I have a few items like this. I'm not sure if this is the BEST way but I know it works.
Set a variable on the app start:
App = Set(variable_visable, "");
Button code:
Onselect = Set(variable_visable.,"1");
Item that you want visible:
Visibility = If(variable_visable="1", true, false);
Edit: You can reset your variable at any point to hide that section.
Sometimes power apps fights you on things that seem correct.
The Visible will the condition that is true to make it show.
For example
If I have one TextBox named TextInput1 and I want a control to be visible when the Text entered = true it will be. For this example use a label.
Label1's visible function will be TextInput1.Text = "true"
This will show when the input text will be true. if it's false or anything else the label won't show. This is a very basic use of the visible but can be used in many ways.
In On select property of button you can't set any other control property directly.
you need to follow the steps as:
1- you need to set a boolean type variable on OnSelect of button e.g.
Set(varShowMail,false)
2- go to TextInput Mail and select its Visible property and assign the variable "varShowMail"
It will work 100%.
Set on visible of screen property UpdateContext({ Var_Visible: false})
set a variable on control "select" or "change" to true"UpdateContext({ Var_Visible: true})" and use the variable in other control visible property that you want to show or hide, if required you can use condition to set a variable to true or false

Hide and unhide series in a chart based on their name and i have error 1104

I am trying to hide and unhide series in a chart based on their name using excel vba and I have a error 1004 invalid parameter after the first run of the for cycle.
Sub macroChart3()
'
' macroChart3 Macro test
'
Dim i, n As Integer
For i = 1 To 96 Step 1
If ActiveChart.SeriesCollection(i).Name = "*contracted*" Then
ActiveChart.SeriesCollection(i).IsFiltered = False
Else
ActiveChart.SeriesCollection(i).IsFiltered = True
End If
Next i
End Sub
SeriesCollection.IsFiltered method seemed to toggle this check box:
I couldn't use that. I wanted to record macro and see if any other method is used but the checkbox is gone in 2010:
So it might be not possible to use that anymore. If you are using a different version where this method exists you might have a problem that the series is not listed in in seriesCollection anymore:
Remarks from MSDN
When a user filters out a series, the series IsFiltered property switches to True, and the series is transferred out of its parent SeriesCollection.
See if you can use FullSeriesCollection instead when you change the visibility of series:
ActiveChart.FullSeriesCollection(2).IsFiltered = True
If that doesn't work you might add and remove ranges instead of hiding them.
UPDATE:
Run your macro in step mode (F8) so you have full visibility of the execution steps. Add your evaluated expressions (ones that are used within IFs) to see their result and you will find out if they are incorrect or are evaluated as FALSE and try to figure out why.
And don't forget to up vote the answer if you find it helpful.
thanks for the quick reply, the error is fixed, but still not all the series are processed by the if clauses in the for cycle, for example this statement:
Else
ActiveChart.SeriesCollection(i).IsFiltered = True
End If
Isn't executed and I don't understand why.

Change value of check box when its name is the value of a variable

First post so I'll try my best to make it a good one (please tell me if I'm doing it wrong).I can't seem to write the code properly to change the value of a check box or option button on a sheet using a variable.
Suppose the name of the check box name is "Chk1", normally I would write :
Worksheets("AP-1").Chk1.value=X
Where X is (either True or False). Since it is a Loop, I use a range variable called FoundRange and it's value is the name of the check box, so I tried:
dim chkname as string
chkname=foundrange.value
Worksheets("AP-1").Chkname.value=X
I also tried
Worksheets("AP-1").CheckBoxes(chkname).Value = X
and
Worsheets("AP-1").Shapes(chkname).ControlFormat.Value=X
I also had a linked cell that I used to change the value, but when it writes either false or true in it, the check box becomes gray and I need to press F2+Enter in the linked cell to make the check box show the proper value. I also tried to select the linked cell after changing its value and use that code:
Application.SendKeys "{F2}"
Application.SendKeys "{ENTER}"
but it did not work.
Please help me :)
Based on your initial code, it must be an ActiveX control, not a Form one, so:
dim chkname as string
chkname=foundrange.value
Worksheets("AP-1").OLEObjects(Chkname).Object.value = X

Resources