How to call the MsgBox Function - vba

How do I “Call” Vba’s MsgBox function? Is this the same as just using the actual function itself? I want to make a MsgBox function that returns the date and time.

Like this:
MsgBox "It is now " & Format(Now, "yyyy-mm-dd hh:nn"), vbInformation + vbOKOnly, "Current Date and Time"

Is this the same as just using the actual function itself ?
Yes, you may consider it as a regular function in VBA.
I want to make a MsgBox function that returns the date and time
The MsgBox function is for displaying questions or warnings and etc. to users and returning answers like Yes, No, Cancel and etc. Strictly speaking the function displays a message in a dialog box, waits for the user to click a button, and returns an Integer indicating which button the user clicked.
If you need to let users enter any information on on the dialog box you need to use the InputBox function instead. It displays a prompt in a dialog box, waits for the user to input text or click a button, and returns a String containing the contents of the text box. You may then parse the date entered on the dialog box and use it in the code. For example:
Dim Message, Title, Default, MyValue
Message = "Enter a value between 1 and 3" ' Set prompt.
Title = "InputBox Demo" ' Set title.
Default = "1" ' Set default.
' Display message, title, and default value.
MyValue = InputBox(Message, Title, Default)
' Display dialog box at position 100, 100.
MyValue = InputBox(Message, Title, Default, 100, 100)

Related

How to add message box VbCancel? [duplicate]

This question already has answers here:
Trouble with InputBoxes
(5 answers)
Closed 4 months ago.
I have found the Macro for MS Word (as below) from the website https://excelchamps.com/blog/vba-code-search-google-chrome/
Sub GoogleSearch()
Dim chromePath As String
Dim search_string As String
Dim query As String
query = InputBox("Please enter the keywords", "Google Search")
search_string = query
search_string = Replace(search_string, " ", "+")
chromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
Shell (chromePath & " -url http://www.google.com/search?hl=en&q=" & search_string)
End Sub
I would like to close the Message Box when I mistakenly press the Macro button/ran this Macro. However, whatever I press (Yes/No/Close that msgbox), it ignored all of these choices and automatically search what I type in the msgbox.
I tried to add the below code but it doesn't work.
If MsgBoxResult = vbCancel Then
Exit Sub
Else
I am junior to Word Macro. May I ask if anyone have any suggestions to make the msgbox work? (press cancel or close the window and then don't open Chrome and search automatically) Thank you for your kind attention.
Just have a look to the official documentation for InputBox
Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns a String containing the contents of the text box.
(...)
If the user chooses OK or presses ENTER, the InputBox function returns whatever is in the text box. If the user chooses Cancel, the function returns a zero-length string ("").
So (in opposite to MsgBox), the InputBox always returns a String, while vbCancel is a number. You will need to check if the return value is the empty string. Note that you can't distinguish between "the user didn't enter something and pressed [OK]" or "the user pressed [Cancel]".
If MsgBoxResult = "" Then Exit Sub

Input box with another single option in VBA

I have a simple inputBox to enter the ID of an employee:
id = InputBox("Enter ID NUMBER of the employee")
If id = "" Or Not IsNumeric(id) Then
MsgBox "No ID entered"
Call prepareToExit
Exit Sub
End If
Until now it was sufficient for me but now I need to be able to enter another parameter in the dialog box which will represent whether the user will use the default settings of the program or want to enter another menu to choose options (for example to change the default folder to user difined).
Can somebody explain how to add this single option in the input box.
I understood that it cannot be done with InputBox method.
All I need is another button like "Advanced..." to know if the user wants to go to additional settings.
Thanks!
Three options come to mind:
1. Use a tailored userform
2. Add another msgbox to ask about the extra option (yes/no)
3. Include the option in the entered string. This is rather simple here, but for such things, regular expressions can be very useful. It kind of depends on your target audience if this may seem too 'cryptical' for some users.
An example:
Dim rx, rxM
Dim ID As String
Dim IDvalue As Long
Dim optionFlag As Boolean
Set rx = CreateObject("vbscript.regexp")
rx.Pattern = "^(\d+)(\+)?$"
ID = InputBox("Enter ID NUMBER of the employee" & vbCrLf & _
"Add '+' for advanced options, e. g. 100345+")
If Not rx.test(ID) Then
'If ID = "" Or Not IsNumeric(ID) Then
MsgBox "No ID entered"
Call prepareToExit
Exit Sub
Else
Set rxM = rx.Execute(ID)
IDvalue = CLng(rxM(0).submatches(0))
optionFlag = (rxM(0).submatches(1) = "+")
MsgBox IDvalue & vbCrLf & optionFlag
End If

Debugging "Search for Name" Code in VBA

I have solid experience in C++ but am still getting used to the syntax of VBA and I think that's what's tripping me up in my code.
What I'm trying to do is have a button that asks the user for a name. If the name entered is in column B, then tell the user the name was found and select where it is (no problem with this). If the name is not found, then ask if the user wants to try another name (no problem with this, either).
Where I'm having trouble is with the "Cancel" buttons. At any time, I want the user to be able to hit "Cancel" and immediately stop the loop, but stay in the sub because I'll be adding to this later.
Here's the code:
Dim inputName As String
Dim row As Integer
Dim i As Integer
Dim tryAgainResponse As Integer
tryAgainResponse = vbOK
'Ask user for name they would like to replace'
inputName = InputBox("What is the name of the person you would like to find? (First Last)")
'Find the row that the name is located and tell the user where it is'
Do While tryAgainResponse = vbOK
For i = 1 To 1000
If Cells(i, 2).Value = inputName Then
MsgBox ("Found the name! It's located at cell B" & i & ".")
ActiveSheet.Cells(i, 2).Select
tryAgainResponse = 0
Exit Do
End If
Next i
tryAgainResponse = MsgBox("We didn't find the name you were looking for. Please try again.", vbOKCancel)
If tryAgainResponse = vbCancel Then
Exit Do
End If
inputName = InputBox("What is the name of the person you would like to find? (First Last)")
Loop
I've tried plenty of things, but the main error is when you hit cancel for the first MsgBox, it tells you the name was found in the first blank square.
Any help or suggestions would be greatly appreciated! This is my first VBA program, so it's not the prettiest, but it's definitely a lot of fun. Thanks!
I'm not sure if I'm understanding what you're asking for, and I can't comment for clarification, but I think your hang up is that when you click cancel on the INPUT box, your input box is returning a blank string, and the rest of your code is then finding a blank cell.
Use the Application.Input Method, declare your input string as a variant, and test if it is false. If it is, use an Exit Sub to exit the macro. You could also test if your input string = "" and then exit the macro if true with the code you have.
From MrExcel
There are 2 versions of InputBox in VBA.
The InputBox Function is called without an object qualifiier and returns the contents of the text box or a zero-length string ("") if the user clicks Cancel.
The InputBox Method is a member of the Application object, so it is called by using Application.InputBox. It returns the contents of the text box or False if the user clicks Cancel. It is more versatile than the InputBox Function because it has a Type argument which specifies the return data type.
The function InputBox() will return an empty string if cancelled. The empty string will compare equal to the first cell that is empty.
Here's the doc of the function: http://msdn.microsoft.com/en-us/library/6z0ak68w(v=vs.90).aspx

Delete Selected Text in a Text Box

Hey fellow Stackoverflow users,
i'm trying to create a text editor with HTML tag functions (for export) and can't get a solution working. Therefor i created a textbox where the user should be able to insert the text. For this insert function i need the in the title described function to delete the selected text (only the selected text, the text around it stays) inside the textbox. Even SendKeys won't function right.
Please let me know if anyone got an idea, thanks upfront!
EDIT: Here's the corrected code for the bold button with maintextbox as textbox for the user's text:
Private Sub BoldButton_Click()
If maintextbox.SelLength = 0 Then
MsgBox ("Please highlight the text you want to edit!")
Else
SelectionText = maintextbox.SelText MsgBox ("SelText: " & SelectionText)
maintextbox.SelText = "<b>" & _
SelectionText & _
"</b>"
End If
End Sub
Simply:
TextBox.SelText = ""
The .Sel* methods all relate to the text currently selected within the control. SelText = "" replaces the current selection with nothing, thereby deleting it whilst preserving the surrounding unselected text.

Applescript how to display a dialog with text and a variable

I need to display a applescript dialog with text and a number by which i mean the text that it displays would be text and a varible like this display dialog "Enter spelling" and repeatnum buttons{"done"} This is the code I have`isplay dialog "How many spellings are there" buttons {"That amount"} default answer ""
set amount to text returned of result
set repeatdone to 0
repeat amount times
repeatdone = repeatdone + 1
display dialog "enter spelling " and repeatdone buttons {"Ok"} default answer ""
end repeat
When I try this it gives me the error cannot make enter spelling into type boolean, Is there anyway to make a applescript dialog display text and a varible
Try:
display dialog "How many spellings are there" buttons {"That amount"} default answer ""
set amount to text returned of result
set repeatdone to 0
repeat amount times
set repeatdone to repeatdone + 1
display dialog "enter spelling " & repeatdone buttons {"Ok"} default answer ""
end repeat