Excel Macro YesNo message box, different directions for Yes and No - vba

The user has two options in the YesNo Message Box. If No, it performs a certain sequence of filters, but I want to filter another column if the user answers Yes to the message box question. Currently, at "Else", I get an error that reads "Compile error: Function call on left-hand side of assignment must return Variant or Object" If I take out "Else", and the code after it, the macro runs smoothly, but only filters when the user selects No.
If MsgBox("Is This Item Catch Weight?", vbYesNo) = vbNo Then
retval = InputBox("Please Enter PO Cost")
ActiveSheet.Range("$A$1:$CL$293662").AutoFilter Field:=71, Criteria1:="=" & retval
retval = InputBox("Please Enter Net Weight")
ActiveSheet.Range("$A$1:$CL$293662").AutoFilter Field:=41, Criteria1:="=" & retval
Else: MsgBox("Is This Item Catch Weight?", vbYesNo) = vbYes
retval = InputBox("Please Enter PO Cost")
ActiveSheet.Range("$A$1:$CL$293662").AutoFilter Field:=71, Criteria1:="=" & retval
End If
End If

A few things are happening here. : new line character in VBA code so a line like
Else: MsgBox("Is This Item Catch Weight?", vbYesNo) = vbYes
is actually the same as
Else
MsgBox("Is This Item Catch Weight?", vbYesNo) = vbYes
Which is not what you want.
There is also an extra End If at the end. Remove that.
Calling the message box to appear multiple times is probably not what oyu want either. Likely you want to show the message box, get the result then do something with it.
Dim response As VbMsgBoxResult
response = MsgBox("Is This Item Catch Weight?", vbYesNo)
If response = vbNo Then
'do stuff for yes
ElseIf response = vbYes Then
' do stuff for No
End If
I'd also suggest not using ActiveSheet unless you are sure that's what you want.

Related

Displays msgbox Excel VBA

My quest to complete my project is nearly done im just facing one final issue with my code.
Sub DeletePatientCheck()
'check if patient record exists before deleting'
Dim s As Worksheet
On Error Resume Next
'Check if Patient Record already exists'
For Each s In Sheets
If s.Name = Selection Then
Worksheets(s.Name).Activate
Call DeleteRecord
End If
Next s
MsgBox "*No Patient Record Found!*"
End Sub
Sub DeleteRecord()
'Confirm delete?'
Answer = MsgBox("Are you sure you want to delete this Patient Record?",
vbQuestion + vbYesNo, "Delete Patient Record")
If Answer = vbNo Then GoTo Skip
If Answer = vbYes Then
'It's benny, lets just double check'
Answer = MsgBox("Are you absolutely sure!", vbQuestion + vbYesNo, "Delete
Patient Record - AYS")
If Answer = vbNo Then GoTo Skip
If Answer = vbYes Then
ActiveSheet.Delete
Sheets("Menu").Select
MsgBox "*Patient Record has been deleted - If done in error please use
previous document version*"
End If
End If
Skip:
Sheets("Menu").Select
End Sub
Basically, when the user submits a "no" response to the Answer msg box under sub DeleteRecord() the code currently brings it back to sub deletepatientcheck and goes to the msg box "No Patient Record found" . This happens even when a record is found.
What I am trying to do is if a no response is given then bring up a different message box saying "Delete request cancelled" instead of the MsgBox "No Patient Record Found!". But no matter what IF/then function or Skip: i use it always displays the "No patient record found" msg box. Can anyone help? happy to explain further if required. Thanks in advance.
This should work for you. Check the value of Boolean variable Exists before displaying your MsgBox.
Sub 1:
Sub DeletePatientCheck()
'check if patient record exists before deleting'
Dim s As Worksheet
On Error Resume Next
'Check if Patient Record already exists'
Dim Exists As Boolean
For Each s In Sheets
If s.Name = Selection Then
Worksheets(s.Name).Activate
Call DeleteRecord
Exists = True
End If
Next s
If Not Exists Then MsgBox "*No Patient Record Found!*"
End Sub
Sub 2: (Recommendations) You can avoid coding the vbNo by just coding for vbYes, and using the Else statement to address the vbNo.
Also notice that you can avoid using the GoTo Skip: method by just calling the task immediately and then using Exit Sub. This link goes into further detail about Goto.
Sub DeleteRecord()
'Confirm delete?'
Dim Answer As String, Answer1 As String
Answer = MsgBox("Are you sure you want to delete this Patient Record?", vbQuestion + vbYesNo, "Delete Patient Record")
If Answer = vbYes Then
Answer1 = MsgBox("Are you absolutely sure!", vbQuestion + vbYesNo, "Delete Patient Record - AYS")
If Answer1 = vbYes Then
ActiveSheet.Delete
Sheets("Menu").Select
MsgBox "*Patient Record has been deleted - If done in error please use previous document version*"
Else
MsgBox ("Delete Request Cancelled")
Sheets("Menu").Select
Exit Sub
End If
Else
MsgBox ("Delete Request Cancelled")
Sheets("Menu").Select
Exit Sub
End If
End Sub

VBA Access Message Box woes

I followed a few simple comments on how to pop a confirmation box before executing a script, but sadly, if I press yes, the script doesn't run.
Private Sub Overwrite_Btn_Click()
If MsgBox("Yes?", vbOKCancel) = ok Then
Me.Product_Quantity = Me.Quantity_Input
Else
Exit Sub
End If
End Sub
I'm trying to set Product_Quantity equaling Quantity_Input, and although it works without the MsgBox command, it doesn't with it.
What am I doing wrong?
Instead of If MsgBox("Yes?", vbOKCancel) = ok Then try: If MsgBox("Yes?", vbOKCancel) = vbOK Then
Typically the interactions with forms will return one constant from a set of several constants. Those are catalogged in enums. In this case you have several constants in the VbMsgBoxResult class, and vbOK is a constant with value 1, which is returned from clicking the ok button.
Actually, If MsgBox("Yes?", vbOKCancel) = 1 Then would work as well, but it is harder to remember that clicking Ok returns 1 then simply stating a constant named vbOK
In object explorer (F2 on the VBE), searching for VbMsgBoxResult will give all possible results that comes from interacting with a message box.
https://www.techonthenet.com/access/constants/msgbox_ret.php
1) Dim a variable as integer.
2) Check for value of integer equal to 6, or check for vbYess
3) ?????
4) Profit
borrowed from link
Dim LResponse As Integer
LResponse = MsgBox("Do you wish to continue?", vbYesNo, "Continue")
If LResponse = vbYes Then
{...statements...}
Else
{...statements...}
End If
Single line:
If MsgBox("Yes?", vbOKCancel) <> vbOk then Exit Sub
'continue code here.
More Information:
MSDN : MsgBox Function (Office/VBA)

Command Button in msgbox vba possible?

si it possible to add a command button in the msgbox window in vba?
For example, i want to add a cancel button that stops the code rather than continuing it. I could create a new userform, but it would be nice if i save some space and use the msgbox that is already here.
VBA has several different types of MessageBoxes with built in command buttons for this very purpose. The type of buttons included in the message box is declared as the second parameter - MsgBox(Prompt, Buttons as)
The types you are probably interested in are:
vbYesNo
vbYesNoCancel
vbAbortRetryIgnore
vbOkCancel
vbRetryCancel
These Buttons return integer values that need to either be stored or used for comparison.
VBA has these integer answers stored as constants (e.g. vbOK = 1, VbCancel = 2, etc.) See Microsoft Developer Network MsgBox Function for more details on that.
Sub mySub()
Dim answer as Integer
answer = MsgBox("Would you like to choose yes?", vbYesNoCancel)
If answer = vbYes Then
'Do whatever you want
ElseIf answer = vbNo Then
'Do whatever
Else
Exit Sub
End If
End Sub
Try this:
If MsgBox("Cancel or Continue, are you sure?", vbOKCancel) = vbOK Then
'continue whatever you want to do.
End if

Why does this vba MsgBox always return zero?

I have a VBA MsgBox with vbYesNo as the button-type but no matter what button is pushed it always shows a return result as zero. Sorry this isn't the entire code but the below code is a portion of the code that is giving me trouble.
Sub testMsg()
Dim strCheckFolders As Integer ' <== vbyesno MsgBoxResult (7 = no, 6 = yes)
Dim RunThis As Boolean
RunThis = True
Do Until RunThis = False
If strCheckFolders = MsgBox("Do you want to check folders for reports to print?", _
vbYesNo, "Check for Reports to Print") = vbNo Then RunThis = False
Loop
End Sub
Basically what I'm trying to do is write this loop without using the standard phrase if x=vbNo then Exit Sub. I'd rather try to let it run it's course. But that would only work if MsgBox could change the value of RunThis to False.
Any help is really appreciated.
= is used both as an assignment operator and a comparison operator in VBA. You can't assign and compare in a single statement (at least not without the unexpected results you're getting). Separate the assignment from the comparison:
Do Until RunThis = False
strCheckFolders = MsgBox("Do you want to check folders for reports to print?", vbYesNo, "Check for Reports to Print")
If strCheckFolders = vbNo Then RunThis = False
Loop
You could even remove the need for strCheckFolders altogether:
Do Until RunThis = False
If MsgBox("Do you want to check folders for reports to print?", vbYesNo, _
"Check for Reports to Print") = vbNo Then RunThis = False
Loop
I would place the MsgBox in an if statement. Then based on the result do an action
Do Until RunThis = False
If MsgBox("Do you want to check folders for reports to print?", vbYesNo) = vbNo Then
RunThis = False
End If
Loop
End Sub

Visual Basic: Message Box Prompt Twice When I Click On Button

I have problem with my message box. This is part of app which check if serial number has already been used. If yes, it shows error message,when click YES program continues and make some functions, NO button end sub. It works well, but it shows me message box twice and I really don't understand why, please help
If File.Exists(pathSN) Then
Dim Findstring = IO.File.ReadAllText(pathSN)
If Findstring.Contains(Lookfor) Then
Dim msg = "Serial number has already been used"
Dim title = "Error"
Dim style = MsgBoxStyle.YesNo Or MsgBoxStyle.DefaultButton2 Or _
MsgBoxStyle.Critical
Dim response = MsgBox(msg, style, title)
MsgBox(msg, style, title)
If response = MsgBoxResult.Yes Then
wrlayout()
openlayout()
Exit Sub
Else
Exit Sub
End If
End If
End If
...
Dim response = MsgBox(msg, style, title)
MsgBox(msg, style, title)
...
you do not need the second call to MsgBox