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.
Related
My program uses a database in access which consists of BreedID and BreedName. I have a dropdown box in vb.net which shows the name for the user to select, however in my code I would like to store the ID related to that name so that I can send it over to the access database using oledb. Currently this is what it looks like:
Dim BrVar As String = Breed.SelectedItem.Text
But that isn't working! Please help!
You can add hidden columns to your dropdown box, it may already exist. The first column in a dropdown box is column(0) and you can set the width to 0cm. This can be for the ID value. Leaving column(1) for the Name value.
Then you can use Breed.SelectedItem.column(0)
The first thing to do is on the Data tab set up your rowsource to SELECT both the BreedID and BreedName fields (in that order). Then make sure bound column is set to 1.
Then on the Format tab set Column Count to 2 and Column Widths to 0;1.
That will have the result of displaying the BreedName field but using the BreedID field as the value of the combo box.
Use Dim BrVar As Long = Breed.SelectedItem to get the value of BreedID.
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.
I have multiple ActiveX checkboxes in my document in a table. They're all calling the same function.
With Selection.Cells(1).RowIndex and Selection.Cells(1).ColumnIndex I can find out the table cell of the checkbox.
Is it possible that I can get the value of the clicked checkbox aswell?
I could only find this code: ActiveDocument.Shapes(1).OLEFormat.Object.Value. But this code is referencing an individual checkbox.
I need to reference the checkbox that was just clicked.
Is that possible and if yes how?
Try something like:
Dim c As Cell
Set c = Selection.Cells(1)
Debug.Print c.Range.InlineShapes(1).OLEFormat.Object.Value
I am trying to make a reset button that will replace any value the user has selected with the value TOTAL inside a number of comboboxes. Using the record macro i selected the combobox but i can't figure out how to insert the value. The following code gives out the 424 error.
ActiveSheet.Shapes.Range(Array("ComboBox2")).Select.Value = TOTAL
the part that i added to the macro is the .Value=TOTAL
Anyone knows what i should do? Please take note that i don't want to clear the comboboxes; I want to give them a specific value.
In case that combo boxes are Form controls use OLEFormat.Object.ListIndex to select the item.
In case that the combo boxes are ActiveX controls use OLEFormat.Object.Object.ListIndex (note in this case the first item in the list has index 0).
Then iterate through all Combo-boxes you want to reset and set ListIndex to index of item "TOTAL". In this example the item "TOTAL" is on the first place in the list so for Form Combo-box (if used) ListIndex = 1 and for ActiveX Combo-box ListIndex = 0. HTH
(You are probably using ActiveX Combo-boxes, but in the example the older Form Combo-boxes are used as well just for the case).
Sub ResetToTotal2()
Dim combos
Dim combo
Dim comboObject
combos = Array("ComboBox1", "ComboBox2", "ComboBox3")
For Each combo In combos
Set comboObject = ActiveSheet.Shapes(combo).OLEFormat.Object
If TypeName(comboObject) = "DropDown" Then
comboObject.ListIndex = 1
Else
comboObject.Object.ListIndex = 0
End If
Next combo
End Sub
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.