Visual Basic Input Box Loop if nohting - vba

I made a coding about calculation, there are 2 numbers in the 2 different 2 boxes, when I calculate the answer is wrong, there is a msgbox shown "Try again", if correct, the msgbox shown "you are correct", but is insert nothing or words then press enter, it will be shown error.
I want if the inputbox insert nothing then press enter, the inputbox will be shown again to restrick someone insert something into the inputbox and can not insert any words into the inputbox, if insert any words, also the inputbox will be shown again return to empty.
Does anyone can tell me how to solve this problem?
Thank you so much.
Dim a As String
Do While True
a = InputBox("Please enter your answer")
If a = Val(txtnumber1.Text) + Val(txtnumber2.Text) Then
Exit Do
Else
MsgBox("Try again!!!!")
End If
Loop
MsgBox("You are correct!")
End Sub
End Class

Dim a As String
Do While True
Do While a="" Or Not IsNumeric(a)
a = InputBox("Please enter your answer")
Done
If val(a) = Val(txtnumber1.Text) + Val(txtnumber2.Text) Then
Exit Do
Else
MsgBox("Try again!!!!")
End If
Loop
MsgBox("You are correct!")

InputBox in Do...Loop
Sub QnA()
Const Title As String = "Q&A"
Dim Answer As String
Dim TryAgain As Long
Do
Answer = InputBox("Please enter your answer", Title, "")
If Len(Answer) = 0 Then
MsgBox "Nothing entered.", vbExclamation, Title
Exit Sub
End If
If IsNumeric(Answer) Then
If Val(Answer) = Val(txtnumber1.Text) + Val(txtnumber2.Text) Then
Exit Do
End If
End If
TryAgain = MsgBox("Wrong answer (""" & Answer & """). Try again?", _
vbYesNo + vbQuestion, Title)
If TryAgain = vbNo Then Exit Sub
Loop
MsgBox "You are correct!", vbInformation, Title
End Sub

Related

Yes/No permission required

I have created a VBA and have tried to add a MsgBox to confirm I do want to continue. The MsgBox appears but does not respond if I click OK or X.I was hoping to be given a Yes/No choice.
Sub Clear_sheet()
ActiveSheet.Unprotect
Dim AnswerYes As String
Dim AnswerNo As String
AnswerYes = MsgBox("Are you sure?", vbQuestion + YesNo, "User Response")
If AnswerYes = vbYes Then
Range("T32 , AB32").Select
Selection.ClearContents
Range("B4:B32").Select
Selection.ClearContents
Range("W11").Select
Else
End If
ActiveSheet.Protect
End Sub
My code also has many more ranges to ClearContents. I'm wondering if the VBA will be improved with fewer lines with the ranges separated with a comma?
Many thanks for your time.
I have found the answer I needed on Google
ActiveSheet.Unprotect "Password here"
Dim AnswerYes As String
Dim AnswerNo As String
AnswerYes = MsgBox("Do you wish to continue?", vbQuestion + vbYesNo, "User Repsonse")
If AnswerYes = vbYes Then
Code to action entered here
Else
End If
ActiveSheet.Protect "Password here"
End Sub

VBA conditions for a UserForm

I am new on VBA. I want to put some restrictions on my inputs in the UserForm. I want My variable to be a number (the user should select the row Number) and if she/he put text or leave it blank an error message should appear. This is the code I have:
Public Sub AddPolicy_Click()
Dim RowNumber As Integer
RowNumber = TextBox1.Value
If RowNumber = "" Then
MsgBox "Error Row Number- enter a value!", vbOKCancel + vbCritical, "Error"
ElseIf VarType(RowNumber) = vbString Then
MsgBox "Error Row Number- enter a Numerical Value!", vbOKCancel + vbCritical, "Error"
End If
Range("A1") = RowNumber
End Sub
I know that the problem is because I declare my variable RowNumber as an Integer, But if I do not do that the software does not recognize my variable, and show me the error message for text, blank and number. SO I do not know how can I solve this.
Thank you in advance for your help
How about this?
Public Sub AddPolicy_Click()
Dim RowNumber As String
RowNumber = TextBox1.Value
If RowNumber = "" Then
MsgBox "Error Row Number- enter a value!", vbOKCancel + vbCritical, "Error"
ElseIf Not IsNumeric(RowNumber) Then
MsgBox "Error Row Number- enter a Numerical Value!", vbOKCancel + vbCritical, "Error"
End If
Range("A1") = RowNumber
End Sub
Edit: About the declaration of String, integer etc.: I know people tend to be very particular about this. But it really doesnt make a difference efficiency-wise for most macros. Here for example you wont gain anything by declaring it better. Even simply not dim'ing it would work nicely.
You could try this code.
The change event on the textbox will stop you being able to enter anything except numerics.
Private LastText As String '<-- place this at the very top of the module.
Private Sub TextBox1_Change()
With Me.TextBox1
'Allow only whole numbers.
If .Text Like "[!0-9]" Or Val(.Text) < -1 Or .Text Like "?*[!0-9]*" Then
.Text = LastText
Else
LastText = .Text
End If
End With
End Sub
Private Sub TextBox1_AfterUpdate()
'Remove any leading or trailing spaces
'(although the Change event should stop you entering spaces).
Me.TextBox1 = Trim(Me.TextBox1)
End Sub
Private Sub AddPolicy_Click()
With Me.TextBox1
If .Value <> "" Then
'Fully qualify where you want to put the data, or it will
'put it in A1 of whichever sheet happens to be active.
ThisWorkbook.Worksheets("Sheet1").Range("A1") = Me.TextBox1
Else
MsgBox "Invalid or no value entered.", vbCritical + vbOKOnly
End If
End With
End Sub

VBA doing a Vlookup and then a vbyesno box

I have written a VBA that does a Vlookup. It is looking up claim numbers and each caim has a hyperlink in column 3 of the range that leads to the details of the claim, pics etc. The Vlookup returns the relevant hyperlink in column 3 fine, but then I want a vbyesno box to give the user the option to be directed to the hyperlink returned by the Vlookup and view the claim details.
I have managed to get it working OK to direct people to a web address (I have used google in my VBA as as example) Is it possible to change my VBA slightly so instead of google it will change the hyperlink depending on the result of the vlookup.
This is the VBA i have written
Sub check()
On Error GoTo MyerrorHandler:
Dim claim_number As String
Dim here As String
claim_number = InputBox("please enter claim number")
If Len(claim_number) > 0 Then
Set myRange = Range("claims")
Set objShell = CreateObject("Wscript.Shell")
here = Application.WorksheetFunction.VLookup(claim_number, myRange, 3, False)
intMessage = MsgBox("The claim has been partly investigated by Ali. Would"
"you like to view the details of the claim.", _
vbYesNo, "Great news Lee")
If intMessage = vbYes Then
objShell.Run ("www.google.com")
Else
Wscript.Quit
End If
Else
MsgBox "You didn't enter a claim number"
End If
Exit Sub
MyerrorHandler:
If Err.Number = 1004 Then
MsgBox "Claim has not been invsetigated"
End If
End Sub
Thanks for the help guys. I've managed to resolve the issue.
I just used ThisWorkbook.FollowHyperlink (here)
as shown below
Sub check()
On Error GoTo MyerrorHandler
Dim claim_number As String
Dim here As String
claim_number = InputBox("Please enter the claim number")
If Len(claim_number) > 0 Then
Set myrange = Range("claims")
Set objShell = CreateObject("Wscript.Shell")
here = Application.WorksheetFunction.VLookup(claim_number, myrange, 3,
False)
intMessage = MsgBox("This claim has been investigated by Ali. Would you
like to view the details?", _
vbYesNo, "GREAT NEWS LEE")
If intMessage = vbYes Then
ThisWorkbook.FollowHyperlink (here)
Else
Wscript.Quit
End If
Else
MsgBox "You didn't enter a claim number"
End If
Exit Sub
MyerrorHandler:
If Err.Number = 1004 Then
MsgBox "This claim has not been investigated"
End If
End Sub

Error message box select statement to handle yes or no

need help with the yes no portion of the error message, if yes I want the code to be launched again and if no then exit sub.
Public Sub Reset()
Dim pt As PivotTable
Dim slice As Slicer
Application.ScreenUpdating = False
ActiveWorkbook.Model.Refresh
For Each pt In ActiveSheet.PivotTables
pt.RefreshTable
For Each slice In pt.Slicers
slice.SlicerCache.ClearAllFilters
On Error GoTo 0
Next slice
pt.PivotCache.Refresh
Next pt
Error 0:
MsgBox "Sorry, Missing data, do you wish to continue?", _
vbCritical vbYesNo, "Restart process!"
Select Case vbYesNo
Case yes
MergeMultipleSheets
Case Else
Exit Sub
End Select
Application.ScreenUpdating = True
End Sub
Here is yes no portion of your code (assuming that you want to launch module MergeMultipleSheets if yes button is clicked):
Sub Reset()
Dim xlAns As Integer
xlAns = MsgBox("Sorry, Missing data, do you wish to continue?", vbYesNo, "Restart process!")
Select Case xlAns
Case vbYes
' do something
' if You want to call sub MergeMultipleSheets
MergeMultipleSheets
Case Else
' do something
Exit Sub
End Select
End Sub
Quick Example
Sub MsgBx()
Dim xlMsgBox As Integer
Dim Cancel As Boolean
xlMsgBox = MsgBox("Do you want to do this ", vbYesNoCancel)
If xlMsgBox = vbCancel Then
Cancel = True ' Exit
Exit Sub
ElseIf xlMsgBox = vbYes Then
' do something
ElseIf xlMsgBox = vbNo Then
' do something
End If
End Sub

Select Case InputBox doesn't work

I wrote a procedure
Sub message()
Dim answer As Variant
answer = InputBox("Write something")
Range("A1").Select
ActiveCell.value = answer
MsgBox "You wrote: " & answer
End Sub
but when a user clicks "Cancel" it actually doesn't cancel, but clear the cell A1.
I tried something like this:
Sub message()
Dim answer As Variant
answer = Select Case InputBox("Write something")
Case vbOK
Range("A1").Select
ActiveCell.value = answer
MsgBox "You wrote: " & answer
End Sub
but it didn't work.
Here's the solution.
Sub message()
Dim answer As Variant
answer = InputBox("Write something")
If StrPtr(answer) = 0 Then ''if Cancel pressed
Exit Sub
Else ''if OK pressed
Range("A1").Value = answer
MsgBox "You wrote: " & answer
End If
End Sub