I have made a combo box within a userform:
private sub Userform_Activate
cmbLocation.AddItem "Field"
cmbLocation.AddItem "Remote"
cmbLocation.AddItem "Other"
end sub
when a user selects Other I'd like a text box to populate for free form text input. When the user does this, I'd like that text to be the .value that is populating into the worksheet table.
is this even possible?
Create a textbox and set it to .visible = False when the userform is activated. Then using an if statement you can output the data from either the combobox or the textbox.
Private Sub UserForm_Activate()
Me.txtLocation.Visible = False
Me.cmbLocation.AddItem "Field"
Me.cmbLocation.AddItem "Remote"
Me.cmbLocation.AddItem "Other"
End Sub
Private Sub cmbLocation_Change()
If Me.cmbLocation.Value = "Other" Then
Me.txtLocation.Visible = True
Else
Me.txtLocation.Visible = False
End If
End Sub
Private Sub CommandButton1_Click()
If Me.txtLocation.Visible = True Then
ThisWorkbook.Sheet1.Range("A1").Value = Me.txtLocation.Value
Else
Thisworkbook.Sheet1.Range("A1").Value = Me.cmbLocation.Value
End If
End Sub
Modify sheet and range as necessary
Related
I have two textboxes in a userform.
I need that if the user has entered some number in one textbox and then places the cursor in the other textbox, the first text box data is deleted and vice versa.
I was able to lock the other textbox if one has data in it, as a workaround.
I tried to replicate the lock method for deleting the value, but it does not work.
Sub checkTB()
If ConversionForm.Controls("UnitFromEntry").Text <> "" Then
ConversionForm.Controls("UnitToEntry").Locked = True
Else
ConversionForm.Controls("UnitFromEntry").Locked = True
End If
End Sub
I call this sub in the before update event of the text box.
Private Sub UnitFromEntry_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Call checkTB
End Sub
Try this
Private Sub UnitToEntry_Enter()
UnitToEntry.Locked = False
UnitFromEntry.Locked = True
UnitFromEntry.Text = ""
End Sub
Private Sub UnitFromEntry_Enter()
UnitFromEntry.Locked = False
UnitToEntry.Locked = True
UnitToEntry.Text = ""
End Sub
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
I have a table format questionnaire to be filled on Userform. There are some sections that to be answered only by check-boxes.Those check-boxes are exist in both worksheet and userform. When the user click the box in the userform it need to be ticked in worksheet as well(I don't know if it is possible).
You can write some If statements to assign the same value to the worksheet checkbox on the click or change event of the UserForm checkbox.
Something like:
Private Sub CheckBox1_Click()
If Me.CheckBox1.Value = True Then
ThisWorkbook.Sheets(1).CheckBox1.Value = True
ElseIf Me.CheckBox1.Value = False Then
ThisWorkbook.Sheets(1).CheckBox1.Value = False
End If
End Sub
1) Link your checkboxes on your sheet to a cell (whichever cell, even on another sheet)
2) Here is the code you could alter to your liking. Make sure to use it as a clickevent on your checkboxes on your userform:
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then Range("D3").Value = True
If CheckBox1.Value = False Then Range("D3").Value = False
End Sub
3) Outcome will be like:
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