I am creating a form out of excel-ActiveX but I have no idea how I can make the command button disabled if all mandatory fields (textbox and combobox) are empty. I've attached the sample form for your reference.
sample LA Form
Use the *_Change event for each control (and optionally the UserForm_Activate event) to set the .Enabled property for the Control Button.
For example, on a simple UserForm with 1 ComboBox (ComboBox1), 2 TextBoxes (TextBox1 and TextBox2) and 1 CommandButton (CommandButton1), the following code will ensure that CommandButton1 is disabled unless there is data in each of the other 3 controls:
Option Explicit
Private Sub CheckMandatoryFields()
'Is a separate sub to make it easy to edit for all Controls at the same time
If Len(TextBox1.Value) > 0 And Len(TextBox2.Value) > 0 And Len(ComboBox1.Value) > 0 Then
CommandButton1.Enabled = True
Else
CommandButton1.Enabled = False
End If
End Sub
Private Sub CommandButton1_Click()
MsgBox "Button is enabled!"
End Sub
Private Sub ComboBox1_Change()
CheckMandatoryFields
End Sub
Private Sub TextBox1_Change()
CheckMandatoryFields
End Sub
Private Sub TextBox2_Change()
CheckMandatoryFields
End Sub
Private Sub UserForm_Activate()
CheckMandatoryFields
End Sub
If your Buttons and Controls are in a Worksheet, then you need to change UserForm_Activate to Worksheet_Activate. If they are in different worksheets, or some in a Worksheet and others in a UserForm, then you will need to Fully Qualify your references (e.g. Sheet1.Textbox1.Value)
If you are using Form Controls in a Worksheet instead of ActiveX controls, then you can't use the _Change events, and the way you Reference the objects is different:
Sheet1.Shapes("TextBox1").TextFrame2.TextRange.Text 'The value of TextBox1
Sheet1.Shapes("ComboBox1").ControlFormat.Value 'Which Number item in ComboBox1 is selected
Sheet1.Shapes("ComboBox1").ControlFormat.List(n) 'The value of the nth item in ComboBox1
A Form Control button does not have an Enabled property - however, if you have a "Do Nothing" macro, you can bodge it like so:
Sub DoNothing()
'As it says
End Sub
Sub MakeASound()
Beep
End Sub
Sub ToggleButton()
If Sheet1.Shapes("Button 1").OnAction = "Sheet1!DoNothing" Then 'Disable Button 1
Sheet1.Shapes("Button 1").OLEFormat.Object.Font.ColorIndex = 16 'Font colour = Grey
Sheet1.Shapes("Button 1").OnAction = "Sheet1!DoNothing"
Else 'Enable Button 1
Sheet1.Shapes("Button 1").OLEFormat.Object.Font.ColorIndex = 1 'Font colour = Black
Sheet1.Shapes("Button 1").OnAction = "Sheet1!MakeASound"
End If
End Sub
Related
I am trying to auto select a checkbox if any another checkbox is selected. All this checkboxes are on the same sheet and basically i want check box 7 to tick if checkbox 3,4 or 5 is selected.
Private Sub Worksheet_Change(ByVal Target As Range)
If Sheets("Start Page").CheckBox3 = True Or Sheets("Start Page").CheckBox4 = True Or
Sheets("Start Page").CheckBox5 = True Then
Sheets("Start Page").CheckBox7 = True
Else
End If
End Sub
Any help would be greatly appreciated.
Even if your checkboxes are linked to a cell (LinkedCell-property), the Worksheet-Change-event is not triggered when you click on a Checkbox.
You need to catch the Click-Event of the checkboxes. For every checkbox, put a Click-event routine into the sheet module. To prevent that the logic if or if not to set the "calculated" checkbox is repeated, let those event handler call a common routine that does the calculation.
Private Sub CheckBox3_Click()
Call SetMyCheckBox
End Sub
Private Sub CheckBox4_Click()
Call SetMyCheckBox
End Sub
Private Sub CheckBox5_Click()
Call SetMyCheckBox
End Sub
Sub SetMyCheckBox()
Me.CheckBox7.Value = Me.CheckBox3.Value Or Me.CheckBox4.Value Or Me.CheckBox5.Value
End Sub
You should, by the way, consider to give your checkboxes more meaningful names.
I have some option buttons set up such as stp1, stp2, stp3, etc.
I have something that which is active into a cell, I want to be able to re-activate the option button via its name which is placed in the cell B6.
I tried many thing here is an example:
Worksheets("Data").Select
[B6].Value = oSel 'Option button select
oSel.Value = True 'as to re-activate the option button
I recieved the name by using:
For Each Control In Me.Controls
If InStr(Control.Name, "stp") Then
If Control.GroupName = "Beginning" Then
If Control.Value = True Then
oSel = Control.Name
Exit For
End If
End If
End If
Next
If you could help I would really appreciate it, and if I forgot some important information or you just need more... just ask. I am fairly new to VBA and I am experimenting.
Although not entirely sure what you require, this simple 'bare-bones' example should take you forward. It allows you to select a radio button on the userform which updates cell 'B6' and also put the radio button name in cell 'B6' which updates the userform. Cell 'B6' is on a sheet called 'Data'
Assumes a userform called UserForm1 and three radio buttons named stp1, stp2 and stp3 as shown. Note that for this example you should set the ShowModal property of UserForm1 to False and set the 'GroupName' property of each radio button to 'Beginning'. Run the example by selecting/running the UserForm1 module.
Put this code in a general module.
Sub chk()
Set sel = Sheets("Data").Range("B6")
With UserForm1
For Each Control In .Controls
If Control.GroupName = "Beginning" Then
If Control.Name = sel.Value Then
Control.Value = True
Exit For
End If
End If
Next
End With
End Sub
Put this code in the UserForm1 module
Private Sub UserForm_Initialize()
Call chk
End Sub
Private Sub stp1_Click()
With Me
Sheets("Data").Range("B6").Value = "stp1"
End With
End Sub
Private Sub stp2_Click()
With Me
Sheets("Data").Range("B6").Value = "stp2"
End With
End Sub
Private Sub stp3_Click()
With Me
Sheets("Data").Range("B6").Value = "stp3"
End With
End Sub
Put this code in 'Data' Sheet module
Private Sub Worksheet_Change(ByVal Target As Range)
Dim sel As Range
Set sel = Sheets("Data").Range("B6")
If Target = sel Then
Call chk
End If
End Sub
These last pieces of code may introduce you to the concept of 'events' and how to begin using them.
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
I have created a userform that contains two checkboxes. I would like to be able to do different things depending on whether each box is checked or unchecked. However, it seems like no matter what I do, it will always tell me the original value of the checkboxes (false and false). Here is the code attached to clicking CommandButton1:
Private Sub CommandButton1_Click()
ReadData
End Sub
And here ReadData:
Sub ReadData()
Dim myForm As UserForm
Set myForm = UserForms.Add("ComplaintEntryForm")
Debug.Print (myForm!CheckBox1.Name)
Debug.Print (myForm!CheckBox1.Value)
Debug.Print (myForm!CheckBox2.Name)
Debug.Print (myForm!CheckBox2.Value)
End Sub
No matter how the boxes are checked, the immediate window always shows this:
VBA.UserForms.Add("ComplaintEntryForm").Show
CheckBox1
False
CheckBox2
False
I have a screenshot of the whole operation but it won't let me upload it because I'm a new user.
Try this method to load and show the form (this goes in a normal module):
Sub main()
Dim myForm As ComplaintEntryForm
Set myForm = New ComplaintEntryForm
myForm.Show
Set myForm = Nothing
End Sub
In the UserForm's own module, add the following:
Private Sub CheckBox1_Change()
readData
End Sub
Private Sub CheckBox2_Change()
readData
End Sub
Private Sub UserForm_Initialize()
Me.CheckBox1.Value = True
Me.CheckBox2.Value = False
End Sub
Private Sub readData()
Debug.Print Me.CheckBox1.Name
Debug.Print Me.CheckBox1.Value
Debug.Print Me.CheckBox2.Name
Debug.Print Me.CheckBox2.Value
End Sub
I've initialized the two checkboxes to specific values in the Initialize event. This means we are certain about the state the form will start in
I am trying to populate a combobox as it is needed, by doing the population in the DropButtonClick function. As a simple example:
Private Sub cmbAdvisor_DropButtonClick()
cmbAdvisor.Clear
cmbAdvisor.AddItem ("Test1")
cmbAdvisor.AddItem ("Test2")
End Sub
This works fine, and the values are shown in the drop down list. However, when I click on one of them, the drop down list goes away, and the combobox now displays nothing/blank. I would expect it to display the item that I had selected. I'm guessing the problem is that I am doing a Clear inside of this function - but how else would I go about this?
I would recommend not using the DropButtonClick event. Here is an alternative (WAY 1) but if you still want to use DropButtonClick then see WAY 2
Way 1
You can populate the combobox either in the UserForm_Initialize() or a button click event. For example
Private Sub UserForm_Initialize()
cmbAdvisor.AddItem ("Test1")
cmbAdvisor.AddItem ("Test2")
End Sub
or
Private Sub CommandButton1_Click()
cmbAdvisor.Clear
cmbAdvisor.AddItem ("Test1")
cmbAdvisor.AddItem ("Test2")
End Sub
Way 2
If you still want to populate the combobox in DropButtonClick then use this
Private Sub cmbAdvisor_DropButtonClick()
Dim prevPos As Long
'~~> Get the position of current selection
prevPos = cmbAdvisor.ListIndex
'~~> Switch off event so that when we select an
'~~> item, it doesn't run this proc again
Application.EnableEvents = False
cmbAdvisor.Clear
cmbAdvisor.AddItem ("Test1")
cmbAdvisor.AddItem ("Test2")
'~~> Set the selected value
cmbAdvisor.ListIndex = prevPos
'~~> Reset events
Application.EnableEvents = True
End Sub
In Action
For an ActiveX control embedded on a sheet:
The activeX combobox processes the click event on item selection as if it were an actual dropbuttonclick - which means we need a state object.
Dim dropbuttonclicked As Boolean
Private Sub ComboBox1_DropButtonClick()
dropbuttonclicked = Not dropbuttonclicked
If dropbuttonclicked = True Then
ComboBox1.Clear
ComboBox1.AddItem ("Test1")
ComboBox1.AddItem ("Test2")
End If
End Sub
For a VBA ComboBox on a VBA UserForm, you might want to use Enter instead:
Private Sub cmbAdvisor_Enter()
cmbAdvisor.Clear
cmbAdvisor.AddItem ("Test1")
cmbAdvisor.AddItem ("Test2")
End Sub