Dynamically populate values in Listbox in VBA - vba

Thanks in Advance.
I have a listbox(say lstbox) in "Sheet1" and Range of values in column A in "Sheet2".
The listbox should populate values dynamically from Column A in Sheet2. I have tried with named reference also but it is not working.
Any help on this is very appreciated

First you need to create a dynamic name range by going in to "Formulas" Tab in which click on "Name Manager" and insert on "New" button.
Enter the Name for range (I have given the name as TestValue) and then use Following Formula at "Refers To area" as "=OFFSET(Sheet2!$A$1,0,0,COUNTA(Sheet2!$A:$A),1)" without Quotation Mark.
After that go to sheet 1 and add ListBox from ActiveX control. Right click on it go to "View Code".
Erase every thing in it and type the below code.
Private Sub ListBox1_GotFocus()
ListBox1.ListFillRange = "TestValue"
End Sub
That's it. When you click on list box it will populate it.

Related

word vba combo box values not displaying although populated

I am trying to populate Mail Merge Data fields from excel database and I am successful in pulling it, but when trying to add it in user form combo box, it is not displayed. Although combo box is populated with 3 blank rows. Screenshot attached.
Below code is written in Module1. Do I need to write anything in Combobox1_change() in userform as well?
For Each aField In ActiveDocument.MailMerge.DataSource.FieldNames
UserForm1.ComboBox1.AddItem
Next aField
Userform screenshot
You haven't told the userform what to add to the combobox. For example:
For Each afield In ActiveDocument.MailMerge.DataSource.FieldNames
UserForm1.ComboBox1.AddItem afield.Name
Next afield

Dynamic conditions related to radio buttons in excel

How to make the conditions of radio button dynamic? for eg. if i have one radio button "yes" in A100 and the condition related to it is appearing in A103. And, if i am inserting the row i want both(button and its value) to get shifted accordingly .
Sub OptionButton21_Click()
Range("A103") = " Not Applicable "
End Sub
Sub DynamicRange()
'Best used when only your target data is on the worksheet
'Refresh UsedRange (get rid of "Ghost" cells)
Worksheets("Sheet1").UsedRange
'Select UsedRange
Worksheets("Sheet1").UsedRange.Select
End Sub
Thanks in advance
Use a named range
1 -Select the target cell (i.e. A103) and give it a name in the "Name Box" on the top-left of the screen. Suppose you chose the name "MyTargetName"
2- Make the control to move with cells.
Right-click the control --> Format control --> Properties --> Move but dont size with cells
3- In VBA code, refer to the target cell by its name instead of its address:
Sub OptionButton21_Click()
Range("MyTargetName") = " Not Applicable "
End Sub
This will make the referenced target shift correctly, along with the control, whenever new rows are inserted.

Set focus on ComboBox (ActiveX Control) after code execution

I have an Excel file with a combobox (name = "Combobox1"). After running a script (basically pasting the selected value in "the next row" of a column) I want the focus to be reset on the combobox at the end of the script, so doing allowing me to type the next entry in the Combobox without having to click on the ComboBox text field first.
This does the job in Excel 2013 but I would like to have it working in 2007 as well:
Combobox1.Activate
Anyone any idea?
Or:
I can replace the combobox with an in-cell dropdown list (data validation) and the same data validation as the one I have in the combobox at the moment, but then I have another issue:
For a ComboBox you can choose to have the dropdown list active, but for an in-cell data validation that is not the case, at least not if you want to be able to type in the cell after the list is shown with ALT+UP or
Application.SendKeys "%{UP}"
Any idea here?
If this combobox is on a worksheet and not a userform, then "Combobox1.select" should return the focus to the combobox.

give a combobox in Excel a default selected value with VBA

I have a combobox in Excel created as the picture suggests, and it has a range associated to it.
How can I give it a default value from this range, through VBA?
ActiveSheet.Shapes("DropDown1").ControlFormat.Value = "Germany"
I tried the above code, but it doesn't work. I believe the syntax is not correct. Can you please help?
Thank you!
You can use the DropDown type, it doesn't show up in the intellisence when you type.
Dim DropDown1 As DropDown
Set DropDown1 = ActiveSheet.DropDowns("DropDown1")
DropDown1.Value = 1
The value is the index of the dropdown, so 1 is the first in the list etc. Use 0 to have no value selected. Also make sure the name "DropDown1" is the correct name for the control, if you right click the control in the excel sheet you'll see the name for the control to the left of the function bar.

Get dropdown value in VBA and get the name of the dropdown...nowhere to be found?

I created a dropdown by dragging the combo box onto my sheet from the UserForm toolbar. I assigned some values to it from some cells in the book. Now I want some VBA code to access the selected dropdown item's value in the form of a string.
My dropdown contains only text.
Also how do I find the name of this newly created dropdown (it's nowhere in the properties!)?
Dim dd As DropDown
Set dd = ActiveSheet.DropDowns("Drop Down 6")
Set r = Sheet2.Range(dd.ListFillRange)
Set ddValue = r(dd.Value)
NOTES:
DropDown is not a visible class. You
just use it and it works.
To find the name of the dropdown
CONTROL (not userform) just look at
the name box in the top left corner of your screen just above column A.
It says the name of the control when
you right click on your control.-
Sheet2 is where the dropdown list is
populated. So wherever your list data
is.
Hope that helps you all.
Here's how you get the String without needing to know the name:
Dim DD As Shape
Set DD = ActiveSheet.Shapes(Application.Caller)
MsgBox DD.ControlFormat.List(DD.ControlFormat.ListIndex)
This is a clunky way of doing it but it should work:
Dim o As Object
For Each o In Worksheets("Sheet1").Shapes
MsgBox o.Name
Next o
There is also a hidden DropDowns collection member of the Worksheet object that you could iterate over. This will find items inserted from the Forms toolbar but won't find items inserted from the Control Toolbox toolbar
Lance Roberts was almost there. If you don't know the name of the drop down that calls the sub, use this:
Dim dd as DropDown
Set dd=ActiveSheet.Shapes(Application.Caller).OLEFOrmat.Object
Dim ddVal as String
ddVal=dd.List(dd.ListIndex)
I used this to create a generic sub for a form with many drop downs.