What should be the answer 'true and (true or false) and false'? - operators

this statement 'true and (true or false) and false' should return 'false' but I can see the answer as 'true'.

the result of the (TRUE OR FALSE) is TRUE and then you continue to do the TRUE AND TRUE (which is resulting TRUE) the last operation is TRUE AND FALSE, which lead to FALSE

>>> True and (True or False) and False
False
In short answer is False.
If you are wondering why ? Then here is how it is evaluated:
(True or False) = True (Since if either is True, result will be True)
Now you are left with True and True and False = False (since "and" is used, if all are True, then only complete statement can be True, but here, one of them is False, so the whole statement will be False.
For better understanding you can use python interactive mode and experiment various different combinations to understand how it work.

Related

How to add error message inside multiples if conditions

It's probably an easy one but I'm kinda new to programming. I probably did some hard-coding beceause I don't know any better.
I would like to add an error message if the first If statement isn't met. I will give you a simplified example of my code. If you want the actual code, let me know and I will edit my question (but careful, it's long!)
If CheckBox1.Value = True and CheckBox2.Value = False then
(CODE number 1)
Else
MsgBox ("error")
End If
If CheckBox1.Value = False and CheckBox2.Value = True then
(CODE numer 2)
Else
MsgBox("Error")
End if
The problem here is that the If statements keeps coming and the code will run line by line. I am aware of that. But I just don't know any better. So I can't place any msgbox error because the code will keep running and execute the rest.
How Do I fragment theses lines so I can place any messagebox I want for each blocks of if ?
Sorry for my english.
Condense that into an If...ElseIf...End If statement.
If CheckBox1.Value = True and CheckBox2.Value = False then
(CODE number 1)
ElseIf CheckBox1.Value = False and CheckBox2.Value = True then
(CODE numer 2)
Else
MsgBox("Error")
End if
This will check the first statement, and if it evaluates as true, it'll do (CODE number 1). If it's not true, it'll check to see if the second statement is true. If the second statement is true, it'll do (CODE number 2). If the second statement isn't true, it'll do MsgBox("Error")
If you have a BUNCH of these, you can do a Select Case True workaround, but it's generally not advised.

Grouping by year in pivot vba

I'm trying to create a pivot table in vba with a field that groups dates by month/year; the Date field runs successfully, but when I add the code to group it by month/year, it gives me an "object required error". How should I fix this?
With ActiveSheet.PivotTables("APivotTable").PivotFields("Date")
.Orientation = xlColumnField
'Error is here
.LabelRange.Group Start:=True, End:=True, _
Periods:=Array(False, False, False, False, True, False, True)
.Position = 1
End With
try this after End with line.
'Change Range("A5") as per your requirement
Range("A5").Group Start:=True, End:=True, Periods:=Array(False, False, False, _
False, True, False, True)
I ran into this problem myself and was looking for a solution. While Maddy's answer is correct, I really dislike the idea of using a hard coded range instead of the OP's approach of using names.
OP's approach was almost correct. Some additional sleuthing led me to realize the issue is that the Group method must be used on the data of the field you are trying to group, and the OP was using the method on the label of the field.
The solution is very easy. Just move the range being grouped by one cell - from the label to the data using Cells:
.LabelRange.Cells(2,1).Group Start:=True, End:=True, _
Periods:=Array(False, False, False, False, True, False, True)

Word 2013 VBA IF-THEN-ELSE ... If statement not working

I'm trying to create a form where a user can tick a box and paragraphs of text are displayed
I've used the code below, however when I click out of design mode the text disappears (as expected) but when I click the check box it doesn't reappear
Private Sub CHECKbutane_Click()
If (Bookmarks("TEXT_Butane").Range.Font.Hidden = True) Then
Bookmarks("TEXT_Butane").Range.Font.Hidden = False
Else
Bookmarks("TEXT_Butane").Range.Font.Hidden = True
End If
End Sub
When you work with using the Hidden property to hide/show text, make sure that the display of Hidden text is turned off in the Word UI and the display of all non-printing options must also be turned off. The individual optinos are in File/Options/Display; all non-printing characters can be toggled on/off using the "backwards P" in the Home tab.
Of course, if this is a macro to be used by others, no one wants to have to continually go into File/Options/Display to change these settings. Here's a macro that turns on the individual settings for everything except hidden text if the non-printing characters are being displayed.
The display of non-printing characters if then turned off and the display of hidden text is turned on/off according to the state of the checkbox.
Private Sub CheckBox1_Click()
Dim vw As Word.View
Dim bChecked As Boolean
Dim bkm As Word.Bookmark
'If the user is currently viewing non-printing characters
'make sure these are turned on individually so that
'not displaying Hidden text does not affect these settings.
Set vw = Application.ActiveWindow.View
If vw.ShowAll = True Then
vw.ShowParagraphs = True
vw.ShowObjectAnchors = True
vw.ShowTabs = True
vw.ShowHyphens = True
vw.ShowOptionalBreaks = True
vw.ShowSpaces = True
End If
vw.ShowAll = False
vw.ShowHiddenText = False
bChecked = Me.CheckBox1.Value
Set bkm = ActiveDocument.Bookmarks("TEXT_Butane")
If bChecked Then
bkm.Range.Font.Hidden = False
Else
bkm.Range.Font.Hidden = True
End If
End Sub
If this were a professional application, you'd want to store the individual "Show" settings and re-apply them when this document is no longer the active document. But that's very advanced programming...
I got the following code to work for me.
So you could essentially:
Public Hide As Boolean
Sub CHECKbutane_Click()
Bookmarks("TEXT_Butane").Range.Select
If Hide Then
Hide = False
Call Hide
Else
Hide = True
Call Unhide
End If
End Sub
Sub Unhide()
With Selection.find
.text = ""
.Format = True
.Font.Hidden = True
.Replacement.Font.Hidden = False
.MatchWildcards = False
End With
Do While Selection.find.Execute
Selection.Font.Hidden = False
Selection.MoveRight 1
Loop
End Sub
Sub Hide()
With Selection.find
.text = ""
.Format = True
.Font.Hidden = False
.Replacement.Font.Hidden = True
.MatchWildcards = False
End With
Do While Selection.find.Execute
Selection.Font.Hidden = True
Selection.MoveRight 1
Loop
End Sub

Application.Interactive = True fails

In the worksheet SelectionChange event I set :
Application.Interactive = False
.... do work
Application.Interactive = True
For some reason Interactive is not getting set back to true, even though I know the code is being executed.
Excel 2013
Any ideas?
Sub Main()
Debug.Print Application.Interactive
Application.Interactive = False
Debug.Print Application.Interactive
Application.Interactive = True
Debug.Print Application.Interactive
End Sub
Doesn't fail for me... Try that and see more here
Since you've discovered the reason for failing the conclusion could be that the Application.Volatile needs to be set to false on a function that uses it because the Interactive mode blocks the user interface and the Volatile calculates things based on user input. While the user input is blocked you can't expect a function that evaluates the user input to work. One excludes the other - hope that makes sense.
Additionally, check your on error resume next or on error goto <label> statements as the would cause skipping some of the code therefore the Application.Interactive = True would have never get executed.

VBS Script - Do Until (Success = True or Fail = True)

I am trying to uninstall a ClickOnce application programmatically using a VBS script. It works pretty well. But if the uninstall fails I want it to send a response "Application has already been removed".
The below is what I have so far, and it works for the most part. Sometimes the delay is not long enough or another window steals focus and the "OK" for sendkeys does not make it to the window.
--- Full Source Code ---
On Error Resume Next
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "taskkill /f /im TEST.App.UI.exe*"
objShell.Run "rundll32.exe dfshim.dll,ShArpMaintain test.app.ui.application, Culture=neutral, PublicKeyToken=f77d770cef, processorArchitecture=msil"
Do Until Success = True
Success = objShell.AppActivate("Test App")
Wscript.Sleep 500
Loop
objShell.SendKeys "OK"
'Commented out on purpose
'install new
'objShell.Run ""
When tackling problems of logical control flow, it's best to start with a
story/description:
I want to start an uninstall process and wait for it to terminate with
either success or failure (indicated by opening a window with
appropriate title)
Never use comparison against True/False in conditions; that's just error-prone
fat.
Don't rely on VBscript's intelligence to convert non-booleans; use
variables/expressions of appropriate (sub) type in conditionals.
Remember that VBScript doesn't shortcut the evaluation of conditionals:
If This() Or That() Then
will call both functions, even if This returns True (and therefore the
whole clause must be True). So don't be tempted to do:
... Until (objShell.AppActivate("Uninstall - Success") Or _
objShell.AppActivate("Uninstall - Failure"))
When designing a loop, think about when the leave-checking should be done; if
you want to read from a possibly empty file, it makes sense to check early:
Do Until tsIn.AtEndOfStream
sLine = tsIn.ReadLine()
...
Loop
but if you want to get valid input from the user, you have to ask before
you can check:
Do
sInp = ...
Loop While isBad(sInp)
.AppActivate and .SendKeys are every programmer's foes; postpone fighting
against them, until you have the control flow problem solved.
In code:
' Wait Wait Fail Success
' , Array(False, False, True , True ) _ shouldn't happen, but will terminate too
Dim aHappenings : aHappenings = Array( _
Array(False, False, False, True ) _
, Array(False, False, True , False) _
)
Dim aTest
For Each aTest In aHappenings
WScript.Echo Join(aTest)
Dim bOk : bOk = False
Dim bFail : bFail = False
Dim i : i = 0
Do
bOk = aTest(i + 0)
bFail = aTest(i + 1)
WScript.Echo Join(Array(" ", i, bOk, bFail))
i = i + 2
Loop Until bOk Or bFail
WScript.Echo "Done."
WScript.Echo
Next
Output:
False False False True
0 False False
2 False True
Done
False False True False
0 False False
2 True False
Done.
You may exploit the fact, that either success or failure (not both) will happen:
Dim i : i = 0
Dim bDone
Do
bDone = aTest(i + 0)
If Not bDone Then
bDone = aTest(i + 1)
End If
WScript.Echo Join(Array(" ", i, bDone))
i = i + 2
Loop Until bDone