VBA Userform - force list change - excel

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

Related

Excel userform not opening in unchecked form

I need a userform to load the the checkbox values in their unchecked form. They open with the boxes unchecked but they don't seem to be initializing that way. Here is the checkbox code and the open button code I have.
Private Sub cbxAdditional_Click()
If Me.cbxAdditional.Value Then
Me.Width = 524
Me.Height = 238.5
lstDatabase.Width = 480
txbxName.Visible = True
txbxComments.Visible = True
Else
Me.Width = 279
Me.Height = 238.5
lstDatabase.Width = 240
txbxName.Visible = False
txbxComments.Visible = False
End If
End Sub
--open code
Sub openform()
Test.Show
cbxAdditional = False
End Sub
I clearly am writing this wrong, when I opne the user form it starts in the IF form, how do I get it to open in the unchecked form?
Here's a very simple userform:
When you enter a userform, the UserForm_Initialize sub is executed. This sub is NOT automatically created in the code with the userform, but you can create it yourself. Any initialization or set up code should be performed here.
In my example below, I'm showing that the resetting of the checkboxes is a separate sub. I do this as a habit, just in case I have to add a reset button or something.
Option Explicit
Private Sub ClearCheckboxes()
Me.CheckBox1.Value = False
End Sub
Private Sub ResetButton_Click()
ClearCheckboxes
End Sub
Private Sub UserForm_Initialize()
ClearCheckboxes
End Sub

Masking Password using checkbox in VBA Excel

I have a Text box (PinEntry) and a Check box. The Check box will mask the password when checked. However, it only masks once. I understand the logic of the first subroutine that it should mask entries as long as I am still entering values in the Text box.
Private Sub CheckBox1_Click()
If AccessForm.PinEntry.Value = True Then
AccessForm.PinEntry.PasswordChar = ""
ElseIf AccessForm.PinEntry.Value = False Then
AccessForm.PinEntry.PasswordChar = "*"
Else
AccessForm.PinEntry.PasswordChar = "*"
End If
End Sub
Private Sub PinEntry_Change()
CheckBox1_Click
End Sub
The reason your code will only mask when clicked is because you will only ever enter your final Else statement. You are checking if the PinEntry (Textbox) has a boolean value of True or False, which it never will.
No need to call the checkbox click event when the text in the PinEntry Changes. Passwordchar is a property that only needs to be set on the textbox when you want to enable or disable it. You just need to act when the Checkbox is clicked.
This code will show the pin if the checkbox is checked, and apply the mask if it is unchecked.
Private Sub CheckBox1_Click()
If CheckBox1.Value Then
PinEntry.PasswordChar = ""
Else
PinEntry.PasswordChar = "*"
End If
End Sub

Checking and Unchecking a Checkbox

I am stuck with a simple checkbox on excel vba userform. I have the following code in its Click event:
Private Sub CheckBox1_Click()
Me.Label1.Caption = "Checked"
Me.Label1.ForeColor = vbRed
End Sub
When I click the checkbox1 in userform, the Label's caption changes and forecolor too. But when I uncheck it, the label caption does't disappear. Where is my code wrong or am I firing the click event again?
Try this :
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
Label1.Caption = "Checked"
Label1.ForeColor = vbRed
Else
Label1.Caption = "UnChecked"
Label1.ForeColor = vbBlack
End If
End Sub
After you paint it red, it won't turn green unless you buy another pot of paint. Nor will it change the caption it has been given without another instruction. Try this code.
Private Sub CheckBox1_Click()
Dim Cap As String ' = "" at this time
With CheckBox1
If .Value Then Cap = "checked"
Me.Label1.Caption = Cap
Me.Label1.ForeColor = IIf(.Value, vbRed, vbGreen)
End With
End Sub
The code intentionally demonstrates 3 ways by which you might achieve the desired result. The smartest is the one which, at once, uses the least amount of code and is the easiest to read (for you).

Userform Textbox to work like an html textbox

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

excel macro pass data between two forms where focus is set

I have two forms.
form1 has four text boxes and a button for each textbox.
The button would setfocus on the textbox and bring up form2.
form2 is basicly a number keypad that also has a text box so that the user can select a number. That number will go in form2.textbox, which when changed will put the data in form1.textbox1.
The problem I'm having is how to tell form2.textbox to put data in form1.textbox2.
This is what my code looks like:
Public Sub textbox1_Click()
Me.textbox1.SetFocus
numbfrm.Show
End Sub
Private Sub textbox2_Click()
Me.textbox2.SetFocus
numbfrm.Show
End Sub
Private Sub textbox3_Click()
Me.txtactual.SetFocus
numbfrm.Show
End Sub
This is what is in the number form. It contains all of the numbers 1 to 10, but I just put the first three numbers here.
Private Sub Cmd1_Click()
TxtNumber.Value = TxtNumber.Value & "1"
End Sub
Private Sub Cmd2_Click()
TxtNumber.Value = TxtNumber.Value & "2"
End Sub
Public Sub TxtNumber_Change()
passnumber
End Sub
This is in a module:
Sub passnumber()
form1.textbox1.Value = numbfrm.TxtNumber
End Sub
I've been looking through the web to find an easy way to do that.
I tried puting in the module
Sub passnumber()
If form1.texbox1.foucs =true then
form1.textbox1.Value = numbfrm.TxtNumber
Else If form1.textbox2.foucs = true then
form1.texbox2.value =numbfrm.txtnumber
End sub
I have made a workaround for it, I put toggle buttons next to each box and when button is pressed it would mark it as true, and then I told it if that toggle is true it would use certain text box
Sub passnumber()
If form1.option1.value =true then
form1.textbox1.Value = numbfrm.TxtNumber
else
If form1.option2.value =true then
form1.textbox2.Value = numbfrm.TxtNumber
end if
end if
End sub

Resources