Chosen result from Worksheet cell to Userform Combobox - excel

I have Userform that have combobox. Combobox picks range from Workbook and inputs picked up result to cell C79 by this VBA:
Private Sub ComboBox1_Change()
ThisWorkbook.Worksheets("Other Data").Range("C79").Value = Me.ComboBox1.Value
End Sub
The problem is when I open Userform for the second time I can't see picked up result in combobox so I have to pick it up again. How to link cell C79 to Private Sub UserForm_Initialize() so that when I open UserForm, value from C79 will be visible in Combobox1?
I have tried:
Private Sub UserForm_Initialize()
ComboBox1.List = ThisWorkbook.Sheets("Other Data").Range("A79:A81").Value ' This one picks the range
'ThisWorkbook.Sheets("Other Data").Range("C79").Value = ReviewForm.ComboBox1.Value
End Sub

To populate a ComboBox control in a UserForm, use the following
Private Sub UserForm_Initialize()
Me.ComboBox1.Value = ThisWorkbook.Sheets("Other Data").Range("C97").Value
End Sub
Alternatively, you could update this value each time the UF is activated:
Private Sub UserForm_Activate()
Me.ComboBox1.Value = ThisWorkbook.Sheets("Other Data").Range("C97").Value
End Sub
Or, you could update the UF's combobox every time the cell value changes. This is not logical however, since you update the cell with the UF. It would activate itself.

Related

VBA update userform textbox automatically

I am trying to update the userform TextBox2 "number of cells needed" with the value of C2. The user enters the number of parts in the TextBox1 and it updates the cell value A2, but I cant get it to pass the value of C2 to the other text box automatically. There is a simple formula in C2 =(A2*2)+1 but i dont think that should matter.
Private Sub TextBox1_Change()
ThisWorkbook.Worksheets("Sheet2").Range("A2").Value = TextBox1.Value
End Sub
Private Sub TextBox2_Change()
TextBox2.txtEcpNum.Text = CStr(Range("C2").Value)
TextBox2.Show
End Sub
The Textbox2_Change() event handler is not being called when Textbox1_Change() is being called. All you need to do is change Textbox2 after you change Textbox1, ie. in the same event handler. Namely:
Private Sub TextBox1_Change()
ThisWorkbook.Worksheets("Sheet2").Range("A2").Value = TextBox1.Value
TextBox2.txtEcpNum.Text = CStr(Range("C2").Value)
TextBox2.Show
End Sub

Userform: Iniialize until value changes in TextBox

I have UserForm with TextBox. Then there is value in 'Other Data'!C6, in cell C6 on Other Data is formula =D6.
Then by this I am passing value to cell:
Private Sub TextBox3_Change()
ThisWorkbook.Worksheets("Other Data").Range("C6").Value = Me.TextBox3.Text
End Sub
Getting value from cell:
Private Sub UserForm_Initialize()
Me.TextBox3.Text = ThisWorkbook.Worksheets("Other Data").Range("C6").Value
End Sub
Private Sub UserForm_Activate()
Me.TextBox3.Text = ThisWorkbook.Worksheets("Other Data").Range("C6").Value
End Sub
Private Sub UserForm_Click()
Me.TextBox3.Text = ThisWorkbook.Worksheets("Other Data").Range("C6").Value
End Sub
The problem is that number from UserForm is overriding formula in 'Other Data'!C6 while opening UserForm. This is affecting other formulas and calculations in my Excel applivcation. How to prevent 'Other Data'!C6 override until I manually change value in TextBox on UserForm?
Everytime you change the value in the textbox you are triggering the change event and over writing C6. The trick is to stop the triggering in your initialise and other events by turning off EnableEvents while you make your change
Private Sub UserForm_Initialize()
application.EnableEvents = false
Me.TextBox3.Text = ThisWorkbook.Worksheets("Other Data").Range("C6").Value
application.EnableEvents = true
End Sub

Disable a command button if mandatory fields are empty

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

VBA Userform ComboBox instantiation

I have a Problem with a Userform which I called "ComboTest2". It only consists of two Comboboxes. If I instantiate the USerform as an object then the following Code doesn't work in the sense that the second combobox of the Userform doesn't contain the desired data.
Sub FillCombo(ByVal row As Long)
Dim rgCities As Range
Set rgCities = Worksheets("Tabelle2").Range("B2:D2").Offset(row)
ComboTest2.ComboBox2.Clear
ComboTest2.ComboBox2.List = WorksheetFunction.Transpose(rgCities)
ComboTest2.ComboBox2.ListIndex = 0
End Sub
Sub FillMain()
Dim ComboForm2 As ComboTest2
Set ComboForm2 = New ComboTest2
ComboForm2.Show
End Sub
UserForm-Code:
Private Sub CommandButton1_Click()
Me.Hide
End Sub
Private Sub CommandButton2_Click()
Me.Hide
End Sub
Private Sub ComboBox1_Change()
FillCombo ComboBox1.ListIndex
End Sub
Private Sub UserForm_Initialize()
ComboBox1.List = Worksheets("Tabelle2").Range("A2:A5").Value
ComboBox1.ListIndex = 0
FillCombo ComboBox1.ListIndex
End Sub
But if I use the "default instantiation" by VBA which means that I change the FillMain Sub to:
Sub FillMain2()
Dim ComboForm2 As ComboTest2
Set ComboForm2 = New ComboTest2
'ComboForm2.Show
ComboTest2.Show
End Sub
Then everything is fine. Why is that so?
Best regards
It's because FillCombo is referring to the userform by name, and therefore to the default instance (you're actually creating a new instance of the form). If that code is not in the userform, and I'm not sure why you would have it outside the form, you should pass the combobox as an argument to it.

Populate a combobox in DropButtonClick

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

Resources