Userform Textbox to work like an html textbox - excel

I am trying to figure out how to get a textbox in an excel userform to update like an HTML textbox. For instance, if you have a default value set to "First Name" and you click in that textbox, it will disappear and let you type in. Once you click out, if you didn't type anything, it will go back to the default "First Name".
I have found quite a few asking about this same type of thing, but none are working for me. Here is my code so far.
Sub QCToolsForm()
QCTools.Show vbModeless
End Sub
Private Sub RUBSTotalExpense_Enter()
Debug.Print "Enter"
If Me.RUBSTotalExpense.Value = "Total Expense" Then
Me.RUBSTotalExpense.Value = ""
End If
End Sub
Private Sub RUBSTotalExpense_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "Exit"
If Me.RUBSTotalExpense.Value = "" Then
Me.RUBSTotalExpense.Value = "Total Expense"
End If
End Sub
Private Sub UserForm_Initialize()
updateDefault
End Sub
Public Function updateDefault()
If Me.RUBSTotalExpense.Value = "" Then
Me.RUBSTotalExpense.Value = "Total Expense"
End If
End Function
The exit sub is not running until I close the userform. I have also tried lose and getfocus with no luck with those at all. I have also tried before and afterupdate, but they only work the first time. I want it to disappear every time you click in the box if the value is "Total Expense". How do I get the exit sub or something similar to run when I leave the textbox?

I tried using your code for Enter and Exit and it worked for me with a test userform. It's updating the field correctly, and printing "Enter" when you select the textbox and "Exit" when you select something else on the userform. Note that for Exit to trigger you need to give focus to something else on the userform. Simply clicking elsewhere will not take focus from the textbox, you must click on another textbox or other control.
I suggest using Debug.Print to observe exactly when each method you try is being called, so that you can be sure when your code is running.
Private Sub TextBox1_Enter()
Debug.Print "Enter"
If TextBox1.Value = "Total Expense" Then
TextBox1.Value = ""
End If
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "Exit"
If TextBox1.Value = "" Then
TextBox1.Value = "Total Expense"
End If
End Sub

Related

VBA Userform - force list change

I have a Useform which shows a list, see picture, i have forced the userform to always begin by having ComboBox1.text = "Please Select Item".
However, how do i force the user to actually select an item before hitting the Ok Button??
My understanding is that i'll use a ComboBox1_BeforeUpdate sub - however, can't figure out the details.
On activation i disable the OK button
Private Sub UserForm_Activate()
OK.Enabled = False
...
End Sub
Then on Change i reactivate the button
Private Sub ComboBox1_Change()
If ComboBox1.Text <> "Please Select Item" Then
OK.Enabled = True
Else
OK.Enabled = False
End If
End Sub

Run-time error '380' when changing combobox value (using userforms and VBA in Excel)

This is a very simplified example to demonstrate my problem.
Create a userform with a combobox and commandbutton.
Set the style property of the combobox to "fmStyleDropDownList".
Add the code
Private Sub CommandButton1_Click()
ComboBox1.Value = "But then it errors sometimes if I change it here"
End Sub
Private Sub UserForm_Initialize()
ComboBox1.Value = "I can initialize to any value I choose"
End Sub
When I run the userform I can click the command button all day long without getting any errors.
But, when I click on the drop-down arrow of the combobox and the text in the box gets highlighted, then the next time I click the command button I get "Run-time error '380': Could not set the Value property. Invalid property value"
Using style="fmStyleDropDownCombo" is not an option I want to consider. I tried using ComboBox1.ListIndex = -1 to clear the selection but that did not work.
Any ideas on how I can reliably avoid this error?
When you use fmStyleDropDownList style, your ComboBox value must match one of the item in the list but because you did not add any item, your list is currently Null.
To test this, make another ComboBox in your Userform and put this in your Userform:
Private Sub CommandButton1_Click()
Debug.Print "=== Click ==="
Debug.Print "ComboBox1 Type: " & TypeName(ComboBox1.List)
Debug.Print "ComboBox2 Type: " & TypeName(ComboBox2.List)
ComboBox1.Value = "But then it errors sometimes if I change it here"
On Error Resume Next
ComboBox2.Value = "But then it errors sometimes if I change it here"
If Err.Number <> 0 Then Debug.Print "ComboBox2 - Error"
Debug.Print "============="
End Sub
Private Sub UserForm_Initialize()
ComboBox1.AddItem "I can initialize to any value I choose"
ComboBox1.AddItem "But then it errors sometimes if I change it here"
ComboBox1.Value = "I can initialize to any value I choose"
ComboBox2.Value = "I can initialize to any value I choose"
Debug.Print "ComboBox1 Type: " & TypeName(ComboBox1.List)
Debug.Print "ComboBox2 Type: " & TypeName(ComboBox2.List)
End Sub
When you run the Userform, you should see this in the immediate window:
ComboBox1 Type: Variant()
ComboBox2 Type: Null
Clicking the button (as many times as you like) before clicking ComboBox2 and you will notice that the output will still be the same. (and no error too because the List is still Null)
Now click ComboBox2 and click the button again, you will see that the type for ComboBox2.List has now changed from Null to Variant and will actually trigger the error.
===============
So, to avoid this, you need to populate the List first, below shows the AddItem method:
Private Sub CommandButton1_Click()
ComboBox1.Value = "But then it errors sometimes if I change it here"
End Sub
Private Sub UserForm_Initialize()
ComboBox1.AddItem "I can initialize to any value I choose"
ComboBox1.AddItem "But then it errors sometimes if I change it here"
ComboBox1.Value = "I can initialize to any value I choose"
End Sub
As far as I know, the preferred way to set a ComboBox value with such style is to loop through the ComboBox List property, check its value and change the ListIndex once found:
Private Sub CommandButton1_Click()
Dim i As Long
For i = 0 To ComboBox1.ListCount - 1
If ComboBox1.List(i) = "But then it errors sometimes if I change it here" Then
ComboBox1.ListIndex = i
Exit For
End If
Next i
End Sub

How to generate an error when important input text boxes are empty

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.

ComboBox Value Changes

Private Sub Workbook_open()
With Sheet1.ComboBox1
.AddItem "Soccer"
.AddItem "Tennis"
End With
End Sub
I would like to make an if statement such that if the ComboBox1 value changes either from nothing to Soccer/tennis or from one item to another
then
Range("A1").Value = "This learner Plays Sport"
the problem is I don't know how to do the part where the combobox value changes either from nothing to Soccer/tennis or from one item to another
I have tried workbook.change and it gives me an error the closes the whole program.
Instead of workbook change event try the ComboBox change event like this:
Private Sub ComboBox1_Change()
If Sheet1.ComboBox1.Value = "Soccer" Or Sheet1.ComboBox1.Value = "Tennis" Then
Worksheets("Sheet1").Range("A1").Value = "This learner Plays Sport"
End If
End Sub
To get to this change event just double click the combobox from the Worksheet and it will open the VBA editor for you.
This only tells you what the newly selected value is. If you need to know what it was before the change it will be a bit more complicated. You'll need to create a variable to store/track the value to compare against once changed.
If you end up with a long list of sports, I'd suggest using Select Case instead of if statements. For example:
Private Sub ComboBox1_Change()
Select Case Sheet1.ComboBox1.Value
Case "Soccer", "Tennis"
Worksheets("Sheet1").Range("A1").Value = "This learner Plays Sports"
Case "Lacross"
Worksheets("Sheet1").Range("A1").Value = "Something Different"
Case Else
Debug.Print "value not in list"
End Select
End Sub

OptionButton determines value then use in macro

A basic simple VBA question that I havent been able to work out even though there are numerous tutorials about it.
I want a Userform to pop-up, give you two options (OptionButton1 and OptionButton2). You choose one, click ok. And depending on what Option is chosen a certain value is used (in an email, for which I DID finish the macro). For simplifying purposes i now just want the variable 'contents' to be printed in cell A1.
I have these parts so far:
Sub MailOption()
UserForm1.Show
End Sub
Private Sub OptionButton1_Click()
If OptionButton1.Value = True Then Var1 = "Text example 1"
End Sub
Private Sub OptionButton2_Click()
If OptionButton2.Value = True Then Var1 = "Text example 2"
End Sub
Private Sub SendEmail_Click()
Cells(1, 1).Value = Var1
End Sub
There are multiple problems: the variable is not shown in Cell A1 and when i press Send Email the Form is not closed. I am probably doing lots of stuff wrong but its the first time im using a userform. Ty very much
I would simply use this one without handling OptionButton_Click:
Private Sub SendEmail_Click()
Dim res As String
If OptionButton1.Value Then
res = "Text example 1"
ElseIf OptionButton2.Value Then
res = "Text example 2"
Else
res = "Nothing is selected"
End If
'write result in cell
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = res
'close form
Unload Me
End Sub

Resources