This code displays a MsgBox and asks a question. The result is returned in a VbMsgBoxResult and is converted to Boolean.
Dim gobox as VbMsgBoxResult
dim go as boolean
gobox = MsgBox("Question?", vbYesNo, "Descriptive Titel")
If gobox = vbYes Then
go = True
Else
go = False
End If
Is there an easier way to convert VbMsgBoxResult to Boolean?
Maybe just
go = (MsgBox("Question?", vbYesNo, "Descriptive Titel") = vbYes)
Related
I am having trouble finding a way that would look at a user input if the user input is = 10-1 then it brings up the worksheet 10-1. If the user inputs is = 10 then if brings another input box that would then ask the user to enter 1,2 and if the user enters 1 it will bring up 10-1 sheet if he enters 2 then it will bring up 10-2 sheet. Another example is if the user enters 100 it will bring an input saying "enter a load" from 1,2,3 if the user inputs a 3 it will bring up 100-3 sheet. If the user input 4 then it will return a message box saying sheet does not exist.
Code:
Private Function SheetExists(name As String) As Boolean
SheetExists = False
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.name = name Then SheetExists = True: Exit Function
Next
End Function
Private Function ConfirmEndSub()
ConfirmEndSub = MsgBox("Do you really want to QUIT", vbYesNo + vbQuestion)
If ConfirmEndSub = vbYes Then
MsgBox "Thank You Goodbye"
End If
End Function
Sub InputValidation()
Dim str As String
Dim inp As String
Dim ws As Worksheet
Dim reply As Long
str = MsgBox("Do you want to select a dataset (Yes) or a Graph (No)", vbQuestion + vbYesNo)
If str = vbYes Then
inp = InputBox("Please enter a load value (10 or a load and trial (10-1)")
If StrPtr(inp) = 0 Then
reply = ConfirmEndSub
If reply = vbYes Then Exit Sub
ElseIf SheetExists(inp) Then
Worksheets(inp).Activate
Else
MsgBox "This load and test cannot be found"
End If
ElseIf str = vbNo Then
inp = InputBox("Please enter a load value (10 or a load and trial (10-1)")
If StrPtr(inp) = 0 Then
reply = ConfirmEndSub
If reply = vbYes Then
Exit Sub
End If
End If
End Sub
Hoping to get my program to work
I am having trouble coding this VBA code. I need the code to ask the user if they want data or a graph. If the user selects Yes From there, I need to the code to look at the selected input and see if that input is a valid sheet name. If not, the input box will display again until valid sheet name. If the sheet is valid, then I need the sheet to be selected or show up whenever the user enters a valid value. I hope that makes sense.
For example, if the user enters (10-1) that is a valid sheet or (1-1) valid sheet but if it is (14-1) or (a-a) that is not a valid sheet.
Note I have not gotten to the graphing part yet, so do not worry about if the user selects no yet. Can someone get me in the correct direction?
Sub InputValidation()
Dim str As String
Dim inp As String
Dim ws As Worksheet
str = MsgBox("Do you want to select a dataset (Yes) or a Graph (No)", vbQuestion + vbYesNo)
If str = vbYes Then
inp = InputBox("Please enter a load value (10 or a load and trial (10-1)")
If StrPtr(inp) = 0 Then
If MsgBox("Do you really want to QUIT", vbYesNo + vbQuestion) = vbYes Then MsgBox "Thank You Goodbye"
Exit Sub
End If
ElseIf inp = "#-#" Or "##-#" Then
If Sheets(ws).Name = inp Then
Worksheets(inp).Activate
End If
Else
MsgBox "This load and test cannot be found"
If str = vbNo Then
inp = InputBox("Please enter a load value (10 or a load and trial (10-1)")
If StrPtr(inp) = 0 Then
If MsgBox("Do you really want to QUIT", vbYesNo + vbQuestion) = vbYes Then MsgBox "Thank You Goodbye"
Exit Sub
End If
End If
End If
End Sub
Please make sure you close all you if statements when using indentation?
The way you have now set it implies that the last End if is covering the entire str = vbYes scope, i.e. if str = vbNo nothing happens?
Sub InputValidation()
Dim str As String
Dim inp As String
Dim ws As Worksheet
str = MsgBox("Do you want to select a dataset (Yes) or a Graph (No)", vbQuestion + vbYesNo)
If str = vbYes Then
inp = InputBox("Please enter a load value (10 or a load and trial (10-1)")
If StrPtr(inp) = 0 Then
If MsgBox("Do you really want to QUIT", vbYesNo + vbQuestion) = vbYes Then MsgBox "Thank You Goodbye"
Exit Sub
'End If
ElseIf inp = "#-#" Or "##-#" Then
If Sheets(ws).Name = inp Then
Worksheets(inp).Activate
End If
Else
MsgBox "This load and test cannot be found"
End If '=> added this End If as it will otherwise skip to end of sequence
ElseIf str = vbNo Then
inp = InputBox("Please enter a load value (10 or a load and trial (10-1)")
If StrPtr(inp) = 0 Then
If MsgBox("Do you really want to QUIT", vbYesNo + vbQuestion) = vbYes Then MsgBox "Thank You Goodbye"
Exit Sub
End If
End If
End Sub
I also think your below line is incorrect?
ElseIf inp = "#-#" Or "##-#" Then
It will give a Type Mismatch error? (Error 13)
You could contemplate using a regex for this assessment or simply use something like the below?
ElseIf inp like "*-*" Then
Which would also do the trick?
The subsequent statement tries to select the Worksheet but that won't work as you have not set the ws object anywhere? So the below line is incorrect in many ways:
If Sheets(ws).Name = inp Then
Please see below code that will give you a good base to start from?
This would also get rid of the unnecessary inp = "#-#" comparison
Private Function SheetExists(name As String) As Boolean
SheetExists = False
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.name = name Then SheetExists = True: Exit Function
Next
End Function
Private Function ConfirmEndSub()
ConfirmEndSub = MsgBox("Do you really want to QUIT", vbYesNo + vbQuestion)
If ConfirmEndSub = vbYes Then
MsgBox "Thank You Goodbye"
End If
End Function
Sub InputValidation()
Dim str As String
Dim inp As String
Dim ws As Worksheet
str = MsgBox("Do you want to select a dataset (Yes) or a Graph (No)", vbQuestion + vbYesNo)
If str = vbYes Then
inp = InputBox("Please enter a load value (10 or a load and trial (10-1)")
If StrPtr(inp) = 0 Then
Reply = ConfirmEndSub
If Reply = vbYes Then Exit Sub
ElseIf SheetExists(inp) Then
Worksheets(inp).Activate
Else
MsgBox "This load and test cannot be found"
End If
ElseIf str = vbNo Then
inp = InputBox("Please enter a load value (10 or a load and trial (10-1)")
If StrPtr(inp) = 0 Then
Reply = ConfirmEndSub
If Reply = vbYes Then Exit Sub
End If
End If
End Sub
This leaves entirely out of consideration that depsite the initial vbYesNo answer the user will always get the input box? Not sure if that is the intention, but alas that is how it was written.
One other consideration is that if the user selects not to Quit, the call is not returning to the request for input? => I.e. it is still ending the routine...
The question asks to create a loop asking an initial yes/no question: are you saving for college? If no, the loop should end. If Yes, the code should ask the user to answer: Years until college starts, annual college payment, and another child? If the answer for another child is yes, the loop should start over. If the answer is no, the loops should end. I am having trouble putting the pieces together.
I have tried using yes/no message boxes but run into the issue of changing to numerical answers and getting the loop to start over.
Ans = MsgBox("Saving for college?", vbYesNo)
If Ans = vbNo Then Exit Sub
If Ans = vbYes Then
Dim myvalue As Integer
myvalue = InputBox("Years until college starts?")
Dim value As Integer
value = InputBox("Annual College Payments?")
Ans = MsgBox("Another child?")
Ans = MsgBox(msg, vbYesNo)
If Ans = vbYes**strong text** Then
Option Explicit
Sub Questions()
Dim ans As Long
Dim yearsToCollegeStart As String, annualCollegePayment As String
Do
If MsgBox("Saving for college", vbYesNo + vbQuestion) = vbNo Then Exit Sub
yearsToCollegeStart = InputBox("Years until college starts?")
annualCollegePayment = InputBox("Annual College Payments?")
ans = MsgBox("Another child", vbYesNo + vbQuestion)
Loop while ans = vbYes
End Sub
The below code may help you. However have in mind that you have to handle user answers (yes, no, close, cancel) in both message boxes & inputboxes.
Option Explicit
Sub test()
Dim Ans1 As Long, value1 As String, value2 As String, Ans2 As String
Ans1 = MsgBox("Saving for college", vbYesNo + vbQuestion)
If Ans1 = vbNo Then
Exit Sub
ElseIf Ans1 = vbYes Then
value1 = InputBox("Years until college starts?")
value2 = InputBox("Annual College Payments?")
Ans2 = MsgBox("Another child", vbYesNo + vbQuestion)
If Ans2 = vbYes Then
'Code
ElseIf Ans2 = vbNo Then
'Code
End If
End If
End Sub
I have two If statements that follow each other down below. The first checks if the previous step is marked as complete with an "X", and exits the macro as appropriate. The second If statement checks if that step has already been run. This displays the "Proceed?" question, but choosing "no" does not end the macro.
If Range("D4").Value = "" Then
Dim response As VbMsgBoxResult
response = MsgBox("Previous step is not marked as complete. Proceed?", vbYesNo)
If response = vbNo Then
Exit Sub
End If
End If
If Range("D4").Value = "X" Then
Dim response2 As VbMsgBoxResult
response2 = MsgBox("Current step is already marked as complete, proceed?", vbYesNo)
If response = vbNo Then
Exit Sub
End If
End If
Set Range1 = Sheets("Latest Open QNs Report Data").ListObjects("OpenQns").DataBodyRange.Offset(0, 1)
Set Range1 = Range1.Resize(Range1.Rows.Count, Range1.Columns.Count - 1)
Range1.ClearContents
Sheets("Instructions").Select
Range("D5", "D5").Value = "X"
You have a typo, you want:
If response2 = vbNo Then ...
Or rename response2 = MsgBox(...) to response = ...
My original question was how to use a function's output in an if-->then statement, and Shai's help was very helpful (here: Using an output of aformula in another).
What I would like to do now, though, is to use this function in a sub. So I have this sub (which is not complete for now):
Private Sub CommandButto1_click()
Dim answer As Integer
Dim Response As VbMsgBoxResult
Dim late As VbMsgBoxResult
answer = MsgBox("Price for only one product?", vbYesNoCancel + vbQuestion, "Payment")
If answer = vbYes then
late = MsgBox("Is the customer late and has to be charged extra?", vbQuestion + vbYesNoCancel)
If late = vbYes then
MsgBox "mergesize function here"
End If
End If
End Sub
It works fine as it is, but where it says - MsgBox "mergesize function here" is where I would like to add my function that looks like this:
Public Function MergeSize(r As Range) As Long
MergeSize = r(1).MergeArea.Cells.Count
If MergeSize <= 10 Then
MergeSize = MergeSize * 70
Else
MergeSize = MergeSize * 65
End If
End Function
Another side question is can I send the output of the function to null and have it only displayed in a msgbox?
Try something like the code below.
Iv'e marked where I added the code that calls the Function MergeSize. I've used Range("B2") as the Merged Range.
Code
Private Sub CommandButto1_click()
Dim answer As Integer
Dim Response As VbMsgBoxResult
Dim late As VbMsgBoxResult
answer = MsgBox("Price for only one product?", vbYesNoCancel + vbQuestion, "Payment")
If answer = vbYes Then
late = MsgBox("Is the customer late and has to be charged extra?", vbQuestion + vbYesNoCancel)
If late = vbYes Then
'===== Added the 3 lines below =====
Dim ExtraCharge As Long
ExtraCharge = MergeSize(Range("B2")) '<-- Range("B2") is a Merged Cells
' === Ver 2.0 - to use with ActiveCell ===
ExtraCharge = MergeSize(ActiveCell) '<-- ActiveCell is a Merged Cells
MsgBox "Extra Charge is " & ExtraCharge
End If
End If
End Sub