I'm setting up an office prank for April Fools that when a question is answered wrong a pop up (Msgbox) will pop up and I want it so it doesn't go away. The idea is their screen would get clogged with these msgboxes till they get the questions right. I'm not sure how to accomplish this though. Any help would be greatly appreciated!
Here is the current code:
Sub Button1_Click()
Dim ws As Worksheet
Dim a As String
Dim b As String
Dim c As String
Dim Ret As Variant
'Lockout Functions
'Cancel = True
'Hal2001 Takes Over
Set ws = ThisWorkbook.Sheets("Hal2001")
Sheets("Hal2001").Visible = True
Sheets("Hal2001").Select
Ret = MsgBox("Would you like to play a game?", vbYesNo)
Application.Speech.Speak "I'm sorry I cannot let you do that, Would you like to play a game?"
If Ret = vbNo Then
Application.Speech.Speak "Well I want to play a game, so we are going to play one"
Else
Application.Speech.Speak "Then Lets Begin"
End If
'First Question
a = Application.InputBox("The Declaration of Independence was signed on what day?")
If a = "July 2nd 1776" Then 'continue
Else
Do While a = Application.InputBox("The declaration of independence was signed on what day?") < 100
Application.Speech.Speak "Are you even trying?"
MsgBox "You really don't know when the Declaration of Independence was signed??"
Loop
End If
'Second Question
b = Application.InputBox("Finish this Sequence 1123_813__")
If b = "1123581321" Then 'Continue
Else
Do While b = Application.InputBox("Finish this Sequence 1123_813__") < 100
Application.Speech.Speak "10, 9, 8, 7, 6, 5, 4, 3, 2, 1!"
MsgBox "Hi, you got that answer wrong"
Loop
End If
'How about some music
Application.Speech.Speak "How about some music?"
Ret = MsgBox("How about some music?", vbYesNo)
If Ret = vbNo Then
Application.Speech.Speak "Too bad, here is one from the eighties you will like."
Shell ("C:\Program Files (x86)\Google\Chrome\Application\Chrome.exe -url https://www.youtube.com/watch?v=oHg5SJYRHA0")
Else
Shell ("C:\Program Files (x86)\Google\Chrome\Application\Chrome.exe -url https://www.youtube.com/watch?v=oHg5SJYRHA0")
End If
'Third Question
c = Application.InputBox("What are the next three numbers 1,4,9,16,?")
If c = "1,4,9,16,25,36,49" Then 'continue
Else
Do While c = Application.InputBox("What are the next three numbers 1, 4, 9, 16, ?") < 100
'Application.Speech.Speak "Terrible!"
MsgBox "Hi, you got that answer wrong. Don't you love these pop up boxes?"
Loop
End If
'Unlock/Return Control
Application.Speech.Speak "Congradulations! You survived our April Fools
Joke! Happy April Fools!"
ActiveWindow.SelectedSheets.Visible = False
'Cancel = False
End Sub
This example should help you:
Option Explicit
Public Sub TestQuestion()
Dim StopAfter As Long
StopAfter = 100 'to stop after 100 times asking
Do While Application.InputBox("The Declaration of Independence was signed on what day?") <> "July 2nd 1776" And StopAfter > 0
Application.Speech.Speak "Are you even trying?"
MsgBox "You really don't know when the Declaration of Independence was signed?"
StopAfter = StopAfter - 1
Loop
End Sub
Just to give you an idea how this could be frustrating to someone:
My sheet:
My Macro:
Dim X As Double
Option Explicit
Sub Test()
With ActiveWorkbook.Sheets(1)
For X = 2 To 4
Do While .Cells(X, 4) <> .Cells(X, 3)
.Cells(X, 4) = Application.InputBox(.Cells(X, 2))
Loop
Next X
End With
End Sub
:)
Related
When I on the step F8 click. then say it Goto is not defined. I try to make a inputbox with a messagebox that me the answer give. And I try also to make code when the values not correct is. See, you where I make a mistake in my VBA code:
Sub TwoNumbersinputbox()
Dim bytAnswer1 As String
Dim bytAntwer2 As String
Dim Range As Byte
Dim strNumber1 As String
Dim strNumber2 As String
[C3] = "Number1"
[C4] = "Number2"
Start1:
strNumber1 = InputBox("Give number one?", "Invoer", 5, 567, 567)
If IsNumeric(strNumber1) Then
MsgBox "This must be Number1", vbCritical, _
"Number1 input"
GoTo strNumber1
Else: [B2] = strNumber1
End If
If Not IsNumeric(strNumber1) Then
MsgBox "there is error.", vbCritical, "Number2 input"
bytAnwer1 = MsgBox("Start Again?", vbYesNo)
If bytAnwer1 = vbYes Then GoTo Start
End If
Start2:
strGetal2 = InputBox("Give Number2?", "Input", 5, 567, 567)
If IsNumeric(strNumber2) Then
MsgBox "This must be Number2 ", vbCritical, _
"Number2 input"
GoTo strNumber2
Else: [B3] = strNumber2
End If
If Not IsNumeric(strGetal2) Then
MsgBox "Is there an error.", vbCritical, "Number2 input"
bytAnswer2 = MsgBox("Start Again?", vbYesNo)
If bytAnswer2 = vbYes Then GoTo Start
End If
End Sub
First thing first - never use GOTO. Only in error handling (On Error statement (VBA)).
Second - if you need to use it, a mark is needed. E.g., if it is GoTo somewhere, then in the code it should be defined like this - somewhere:.
Sub DontUseGoTo()
Dim i As Long
i = 0
somewhere:
i = i + 1
Debug.Print i
If i < 10 Then
GoTo somewhere
End If
End Sub
I'm trying to setup something to have a little fun with my co-workers where when they click a button in Excel it will force them to answer a bunch of math problems before unlocking their system. If they get it wrong I want it to play a funny sound.
That said I'm trying to figure out the best way to handle this and I'm not finding a lot online that does what I'm looking for.
What I have so far isn't working. I keep getting the following error:
"Expected sub function or property"
What I have so far is pretty basic, any help would be greatly appreciated though.
Sub Button1_Click()
Dim ws As Worksheet
Dim a As String
Dim b As String
Dim c As String
Dim URL As String
'Lockout Functions
'Cancel = True
'Check for Muted Sound, Unmute
'First Question
a = Application.InputBox("What is 1+1?")
If a = "2" Then 'continue
Else goto a
Application.Speech.Speak "Are you even trying?"
MsgBox "Hi, you got that answer wrong"
End If
'Second Question
b = Application.InputBox("Finish this Sequence 1123_813__")
If b = "1123581321" Then 'Continue
Else goto b
Application.Speech.Speak "It's the Fibonacci duh!"
MsgBox "Hi, you got that answer wrong"
End If
'Third Question
c = Application.InputBox("What are the next three numbers 1, 4, 9, 16, ?")
If c = "1,4,9,16,25,36,49" Then 'continue
Else goto c
Application.Speech.Speak "Terrible!"
MsgBox "Hi, you got that answer wrong"
End If
'Unlock/Return Control
'Cancel = False
End Sub
You do not need a, b and c into the ELSE
Sub Button1_Click()
Dim ws As Worksheet
Dim a As String
Dim b As String
Dim c As String
Dim URL As String
'Lockout Functions
'Cancel = True
'Check for Muted Sound, Unmute
'First Question
a = Application.InputBox("What is 1+1?")
If a = "2" Then 'continue
Else
Application.Speech.Speak "Are you even trying?"
MsgBox "Hi, you got that answer wrong"
End If
'Second Question
b = Application.InputBox("Finish this Sequence 1123_813__")
If b = "1123581321" Then 'Continue
Else
Application.Speech.Speak "It's the Fibonacci duh!"
MsgBox "Hi, you got that answer wrong"
End If
'Third Question
c = Application.InputBox("What are the next three numbers 1, 4, 9, 16, ?")
If c = "1,4,9,16,25,36,49" Then 'continue
Else
Application.Speech.Speak "Terrible!"
MsgBox "Hi, you got that answer wrong"
End If
'Unlock/Return Control
'Cancel = False
End Sub
I am trying to find a way to avoid using GoTo loops in VBA as i understand they can lead to serious confusion and issues. I have a user InputBox, where the user defines a variables, currently on the if statement there are 3 options, if = Y ElseIf = N and else GoTo Start.
However this works great for the case where the user mistypes the variable i.e Y# etc, but i currently run into issues when the user wants to close the input box i.e clicks cancel or the cross.
So i was wondering if there is a more elligent solution to this or am I stuck with this hicup?
My code is below, this is only a test set used to test this new feature i am adding to my main code.
Sub MsgBox_Test ()
Dim TestVariable As String
VariableEntrey: TestVariable = InputBox(" Y or N")
If TestVariable = "Y" Or TestVariable = "y" Then
MsgBox ("Yeyy")
ElseIf TestVariable = "N" Or TestVariable = "n" Then
MsgBox ("Awww")
Else: GoTo VariableEntrey
End If
End Sub
Thanks for any help you are able to provide
You can try a Do... Loop Until construct that just repeats until you get an acceptable answer. For example:
Sub GetAnswer()
Dim strAnswer As String
Do
strAnswer = InputBox("Y or N")
Loop Until strAnswer Like "[YyNn]"
MsgBox "Thanks for your answer of: " & strAnswer
End Sub
See the docs on the Like operator to prevent having to check individually for y, Y, n and N.
Option Explicit
Sub MsgBox_Test()
Dim TestVariable As String
Dim done As Boolean
Do
TestVariable = InputBox(" Y or N")
done = True ' preload exit value
If LCase(TestVariable) = "y" Then
MsgBox ("Yeyy")
ElseIf LCase(TestVariable) = "n" Then
MsgBox ("Awww")
ElseIf Len(TestVariable) > 0 Then
done = False ' abort exit
End If
Loop Until done
End Sub
I would use a MsgBox instead of an InputBox.
Sub GetYesNo()
Dim answer As VbMsgBoxResult
answer = MsgBox("Yes or No?", vbYesNo)
If answer = vbYes Then
'do something
Else
'do something else
End If
End Sub
You could use Do >> Loop Until loop, to remove the GoTo.
Also, you can use UCase(TestVariable) to remove the Or from your Ifs.
Sub MsgBox_Test()
Dim TestVariable As String
Do
TestVariable = InputBox(" Y or N")
If UCase(TestVariable) = "Y" Then
MsgBox ("Yeyy")
ElseIf UCase(TestVariable) = "N" Then
MsgBox ("Awww")
End If
Loop Until UCase(TestVariable) Like "[YN]"
End Sub
You could try with Do .... Loop here:
Edit: plus limitation for single character or empty input
Dim TestVariable As String
TestVariable = InputBox(" Y or N")
Do While (TestVariable = "N" Or TestVariable = "n" or Len(TestVariable) > 1)
MsgBox ("Awww")
TestVariable = InputBox(" Y or N")
Loop
If TestVariable = "Y" Or TestVariable = "y" Then
MsgBox ("Yeyy")
End if
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!
I am having trouble handling the error associated with a input box "Cancel" click. Or in otherwords, it returns an error within the sub if the value of the input is null. I have tried looking around and still can't seem to get it quite right. Here is my attempt:
Private Sub bttnSavingsExpected_Click()
Dim expected() As String
Dim nPeriods As Integer
Dim counter As Integer
Dim savings As Single
With ActiveSheet.Range("A13")
nPeriods = Range(.Offset(1, 0), .End(xlDown)).Rows.Count
End With
ReDim expected(1 To nPeriods)
counter = 1
For counter = 1 To nPeriods
expected(counter) = Range("A13").Offset(counter, 0).Value
Next
TryAgain:
On Error GoTo ErrH
counter = 1
For counter = 1 To nPeriods
savings = InputBox("How much savings do you expect from " & expected(counter) & "?", "Savings?", Range("A13").Offset(counter, 1).Value)
If savings = "" Then
Exit Sub
Else
Range("A13").Offset(counter, 1).Value = savings
End If
Next
Exit Sub
ErrH:
MsgBox "Please enter value. If the default value is desired then please click 'OK'.", vbOKOnly, "Do Not Click Cancel"
GoTo TryAgain
End Sub
With this attempt, the MsgBox is displayed the first click whether there is a input or not and even if I click "Ok". The second try of clicking "OK" or "Cancel" leads to being kicked back to the editor.
You've got Dim savings As Single and If savings = "" Then. Thats always going to error
Try using Dim savings As Variant
Make sure the variable for the Inbox is set at "", then test the value for False. Much easier than anything else I have seen:
Sub WolfPackURL_input()
Dim TheURL As String
Dim SaveURL As Hyperlink
Set savedURL = Sheets("Data").Range("I1")
TheURL = ""
TheURL = Application.InputBox("Input the Sign-Up URL", "Wolfpack Weekly Players URL", "http://something", 1)
If TheURL = "False" Then
Exit Sub
End If
ThisWorkbook.Worksheets("Data").Activate
Sheets("Data").Range("I1").Hyperlinks.Delete
Sheets("Data").Range("I1").ClearContents
Sheets("Data").Range("I1").Clear
ActiveSheet.Hyperlinks.Add anchor:=Sheets("Data").Range("I1"), Address:=TheURL, ScreenTip:="Open file", TextToDisplay:=TheURL
End Sub