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
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...
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)
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
I really searched hours and hours, but I can't find any solutions.
You should only enter numbers into the Inputbox and a msgbox should sppears when you just hit ok without any number or string...
The first part was easy, but I always get an error message by just hitting OK!
Public Sub test()
Dim vntReturn As Variant
vntReturn = Application.InputBox("Bitte Wert eingeben", "Eingabe", , , , , , 1)
If StrPtr(vntReturn) = 0 Then
MsgBox "Abbrechen gedrückt"
Else
If vntReturn = False Then
MsgBox "Nix eingegeben"
Else
MsgBox vntReturn
End If
End If
End Sub
This is happening because you're declaring the Type for this InputBox to a number. So excel will automatically try to correct this. You can use an InputBox without a Type and program your own verification for checking if it's an integer or not.
Otherwise you can also add this before your code:
Application.DisplayAlerts = False
And then set it to True after. Now when you hit ok you won't be prompted with the error, but the InputBox will not go away. You could add additional instructions to the InputBox to make it clear it needs a number.
#mehow: as Alex D just said: Your answers are similar ;-)
First I used the code of mehow, but now I just create a userform with only an "OK-button".
Private Sub Rechnen_Click()
Dim i As Integer ' Variable deklarieren
Dim Sum As Integer
Dim counter As Variant
i = 0 ' deklariert, löst beim Kompilieren keinen Fehler aus
Sum = 0 ' nicht deklarierte Variable löst beim Kompilieren einen Fehler aus
counter = TextBox1.Value
Application.DisplayAlerts = False
If Not IsNumeric(counter) Then
Exit Sub
Else
Unload Userform1
On Error Resume Next
Do Until i >= counter
Zahl = InputBox("Pls enter a number:", i + 1 & ". Versuch")
Sum = Sum + Zahl
i = i + 1
If Not IsNumeric(Zahl) Then
MsgBox "calculation premature cancelled!"
Exit Do
End If
Loop
Ausgabe = MsgBox("Die Summe lautet: " & Sum, vbInformation, "Ergebnis")
Question = MsgBox("is it enough?", vbYesNo + vbQuestion, "repeat")
If Question = vbNo Then
Userform1.Show
Else
Unload Userform1
Exit Sub
End If
End If
End Sub
and so the userform looks like:
Now the program works fine ;) Thank you guys!