I have an Excel-VBA user form which is used to get user input. I need to display this user form when an error occurs so that the user can redefine the input value.
Pseudo-code:
Sub ()
userform.Show
Call Execute
End Sub
Sub Execute()
Validate the input
If input is wrong
MsgBox "reselect the input"
-here I need to disply the userform-
End sub
I tried GoTo Userform which gives me a Label Not Defined error. Any suggestions?
I have created a UserForm :
On command button click, the code checks for the input, and if the input is wrong, it displays an error in message box and then redirects to the userform.
Private Sub CommandButton1_Click()
If TextBox1.Value = "" Then
MsgBox ("Please provide the value")
UserForm1.Show
UserForm1.Repaint
End If
End Sub
PS : Change the ShowModal property of the Userform to "False".
Related
I am trying to prevent users from saving without using the naming conventions established in a Userform. Below is the workbook code:
Public Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
UserForm1.Show
If UserForm1.Visible = True Then
Cancel = False
Exit Sub
End If
Cancel = True
MsgBox ("Please use the save form.")
End Sub
I then have a button on the userform that should save the file containing the code:
Sub SaveButton_Click()
Dim FileName As String
FileName = FileNameTextBox.Value
ActiveWorkbook.SaveAs FileName:=FileName
End Sub
Unfortunately this gives me a
400 error: "Can't show modally".
I am not sure what this means or how to resolve it. Please let me know if there is a better way to do this.
UserForm1.Show is showing the form modally - that means execution will only resume with the next instruction after the form has closed, and this implies If UserForm1.Visible = True will always be False... if you're lucky.
If the user closes the modal form with the red [x] button, then the object gets destroyed, and If UserForm1 re-spawns a new one, and that instance will not be visible either (because it was never shown in the first place).
You could try showing the form modeless:
UserForm1.Show vbModeless
Now execution will resume in this scope immediately after showing the form... making the condition UserForm1.Visible always True.
Remove UserForm1.Show from the BeforeSave handler: that handler's job is to cancel saving if the form isn't visible - not to unconditionally display that form! - you're getting this "can't show modally" error, because you're trying to show a modal form that's already modally displayed.
Read up on userforms and default instances on my Rubberduck blog.
I am creating a data entry userform that calculates loads (e.g.pipeloads, wind loads etc..). There are important text boxes that cannot be left unfilled. If the user clicks the command "Add Input" and there are some text boxes left unfilled, I want an error to be generated that forces the user to enter a value.
I've got as far as generating an error for one text box. This code will trigger an error alert when you try to leave the text box without filling it first.
Private Sub txtdist_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Trim(txtdist.Value) = "" And Me.Visible Then
MsgBox "Required Entry!", vbCritical, "Error"
Cancel = True
txtdist.BackColor = vbRed
Else
txtdist.BackColor = &HC0FFFF
End If
End Sub
What I'm trying to achieve is that when I click "Continue" command button, then the program will see if any textbox or combobox is left empty to force the user to enter a value. (Similar to when you try to buy online with credit card, the page will not submit if there is not name or credit card number etc...). Thank you.
Maybe you can try this
Private Sub Continue_click()
Dim c as Control
For Each c In Me.Controls
If TypeName(c) = "TextBox" Then
If Trim(c.Value) = "" Then
MsgBox "Please fill out all required information", vbInformation
Exit Sub
End If
End If
Next c
End Sub
Since you want the code to check for empty textboxes when you click "Continue" Command Button, you should add your check code in that Sub.
Here's an example on how you can do it:
Sub Continue_click()
'...
If Trim(txtdist.Value) = "" Then
MsgBox "Please fill out all required information", vbInformation
Exit Sub
End If
'...
End Sub
Hope this helps.
I have a Userform with a multipage object. In one of pages there is a textbox object where I want to add a placeholder when Enter event is not detected. Problem is I'm not figure out how to make reference to textbox from Enter and Exit events due to I have a multipage structure.
I tried to adapt the following approach but it works only for a Userform without Multipage structure.
Got it here: Text box prompt text in Excel VBA
Private Sub TextBox1_Enter()
TB_enter ActiveControl.Name
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TB_exit ActiveControl.Name
End Sub
Sub TB_enter(TB_name)
If Len(Me.Controls(TB_name).Tag) = 0 Then
Me.Controls(TB_name).Tag = Me.Controls(TB_name).Value
Me.Controls(TB_name).Value = vbNullString
End If
End Sub
Sub TB_exit(TB_name)
If Len(Me.Controls(TB_name).Value) = 0 Then
Me.Controls(TB_name).Value = Me.Controls(TB_name).Tag
Me.Controls(TB_name).Tag = vbNullString
End If
End Sub
Here is a video that shows how to do it when the Userform don't have a Multipage structure: https://www.youtube.com/watch?v=yJ4fnw1zmGU
Setting up properties Tag and Text of textbox I get the placeholder if use a properly Forecolor (&H8000000) but I can't make it works dynamically, that means: delete placeholder text when Enter event occurs and if lenght (Len) of the string inside textbox is zero when Exit event occurs must show the placeholder string again.
Any suggestions? Thanks in advance.
I'm trying to unload a userform ("DataEntry") when the active cell is not in the appropriate sheet (index number 2). I am getting a "Type Mismatch '13" after the messagebox, and I don't know why, can anyone help?
This sub is in a module, not the userform. I have tried setting up a sub in the userform and calling that (single line sub "Unload Me", but I get the same result.
Sub unloadform()
If ActiveSheet.Index <> 2 Then
MsgBox "The schedule must be selected to enter data"
Unload DataEntry
Exit Sub
End If
End Sub
Update:
I occurred to me to put this code in the userform initialize sub, (I don't want the userform to load if the correct sheet is not selected). Now I get a Runtime ErrorĀ '91'
Tried replacing Unload Me with Unload DataEntry (the name of the userform), (same result), and replacing it with Unload VBA.Userforms("DataEntry"), which throws a Runtime '13' error.
Private Sub UserForm_Initialize()
If ActiveWorkbook.ActiveSheet.Index <> 2 Then
MsgBox "Schedule must be selected to enter data"
Unload Me
Else:
'other code if correct sheet is selected
End If
End Sub
I have a drop down select list in a VBA form I would like to validate as soon the user clicks on it. It needs to check that a pre-requisite drop down has been filled already.
This is to avoid the user jumping ahead on the form because there are certain fields that need to be filled out first. My attempt so far is not working:
Private Sub cbo_moduleName_Click()
If Len(cbo_moduleCode.Value) = 0 Then
MsgBox ("Please select a module code")
Exit Sub
End If
End Sub
It seems the Click event is activated only when the box's value is changed with the mouse, not every time it is physically clicked on. Try this:
Private Sub cbo_moduleName_Enter()
If Len(cbo_moduleCode.Value) = 0 Then
MsgBox ("Please select a module code")
cbo_moduleCode.SetFocus
Exit Sub
End If
End Sub