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
Related
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
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
This question already has answers here:
Disable Excel save option but allow macro save
(2 answers)
Closed 5 years ago.
I am writing a code that will prevent the user from saving the workbook, and it will only save when I want it to. This is to prevent the user from making changes and saving when they are not supposed to. I have created two private subs, but I don't know how to make an exception when the workbook is being saved on my own. I would like to be able to place the saving code in various macros so that I can control the save at any point.
The following is my code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
MsgBox "You can't save this workbook!"
Cancel = True
End Sub
Private Sub Workbook_Open()
Dim myValue As String
Dim Answer As String
Dim MyNote As String
MsgBox "Welcome to the Lot Input Program"
If Range("A1").Value = "" Then
Line:
myValue = InputBox("Please input your email address:", "Input", "x#us.tel.com")
'Place your text here
MyNote = "Is this correct?: " & myValue
'Display MessageBox
Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "Confirmation")
If Answer = vbNo Then
'Code for No button Press
GoTo Line
Else
Range("A1").Value = myValue
End If
ActiveWorkbook.Save
End If
End Sub
You may try something like this...
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Environ("UserName") <> "YourUserNameHere" Then
MsgBox "You can't save this workbook!"
Cancel = True
End If
End Sub
Edit:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim Ans As VbMsgBoxResult
Ans = MsgBox("You can't save this workbook!" & vbNewLine & _
"Do you have password to save the file?", vbQuestion + vbYesNo)
If Ans = vbYes Then
frmPassword.Show 'UserForm to accept the password
Else
Cancel = True
End If
End Sub
I added a public variable saveLock that I reference in the save cancel code. This allows me to lock and unlock the save inside of my code. If anyone has a better way please let me know, but this did solve the problem.
Public saveLock As Integer
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If saveLock = 0 Then
Cancel = True
End If
End Sub
Private Sub Workbook_Open()
Dim myValue As String
Dim Answer As String
Dim MyNote As String
saveLock = 0
MsgBox "Welcome to the Lot Input Program"
If Range("A1").Value = "" Then
Line:
myValue = InputBox("Please input your email address:", "Input", "x#us.tel.com")
'Place your text here
MyNote = "Is this correct?: " & myValue
'Display MessageBox
Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "Confirmation")
If Answer = vbNo Then
'Code for No button Press
GoTo Line
Else
Range("A1").Value = myValue
End If
saveLock = 1
ActiveWorkbook.Save
saveLock = 0
End If
End Sub
I am building a code which gives warning when the workbook is closed. By default excel warns only once (that too if current changes are not saved). I want warning to come twice. First time it should ask are you sure? when the person hits yes, again system should ask are you sure.
So far my code is as follows, but it is not working properly. With the below code, warning is only displayed once. Please can someone help?
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim mbResult As Integer
Dim wkb As Workbook
Set wkb = ActiveWorkbook
mbResult = MsgBox("Are you sure you want to exit this program?", _
vbYesNo)
Select Case mbResult
Case vbYesNo
MsgBox "You are about to exit this program, are you sure?"
Case vbYes
Cancel = True
Case vbNo
' Do nothing and allow the macro to run
Exit Sub
End Select
End Sub
you can directly handle the return value of a MsgBox like this:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Select Case MsgBox("Are you sure you want to exit this file?", vbYesNo)
Case vbYes
Cancel = MsgBox("You are about to exit this program, are you sure?", vbYesNo) = vbNo
Case vbNo
Cancel = True
End Select
End Sub
or, which is equivalent:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Cancel = MsgBox("Are you sure you want to exit this file?", vbYesNo) = vbNo
If Not Cancel Then Cancel = MsgBox("You are about to exit this program, are you sure?", vbYesNo) = vbNo
End Sub
You could use a Workbook_BeforeClose event to prompt the user to check if they really want to do something.
Both these two codes below ask the user twice for confirmation if they want to close the workbook. The first one use the Workbook_BeforeClose event, so you will need to do something in the code to save changes etc.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If MsgBox("Are you sure you want to close the workbook?", vbYesNo, "User Input") = vbYes Then
If MsgBox("Are you relly sure you want to close the workbook?", vbYesNo, "User Input") = vbYes Then
Call MsgBox("No turning back now", vbOKOnly, "User Anwser")
End If
Else
Call MsgBox("That was close", vbOKOnly, "User Anwser")
End If
End Sub
This second set of code goes in a normal module and you could call it from your macro when you want the user to close the workbook. I have put in a workbook save so that changes are not lost.
Sub Close()
If MsgBox("Are you sure you want to close the workbook?", vbYesNo, "User Input") = vbYes Then
If MsgBox("Are you relly sure you want to close the workbook?", vbYesNo, "User Input") = vbYes Then
Call MsgBox("No turning back now", vbOKOnly, "User Anwser")
ActiveWorkbook.Close True
End If
Else
Call MsgBox("That was close", vbOKOnly, "User Anwser")
End If
End Sub
How it looks running,
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