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

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.

Related

Populating a dynamically added listbox from dynamically added combobox selection

The userform has 3 columns, a combobox (list of vendors), and listbox (list of vendors' product / service) and a textbox with the price of the selected product / service.
Example of menu (3rd column item not added)
The user presses a button to add a row, which populates within a scrollable frame a new row of the 3 columns. My goal is to have the listbox's options change according to the selection in the combobox on that row.
I have a listener (see my previous post on combobox(number)_change() to see how this works) which is the following ...
Private Sub ControlHandlerCollection_ComboBoxChange(ComboBox As MSForms.ComboBox)
MsgBox ComboBox.Name
End Sub
I wrote this to test if the change sub would work for ComboBox #87 for example. However, I don't know how to tell the same numbered listbox to change its list options according to the selection of ComboBox#
If I had a listener specifically for ComboBox1_Change() then I'd simply write the change for ListBox1 within it, but I'm writing this for a dynamic 'infinite' amount of ComboBoxes.
My current plan of attack would be to write within the sub in the code box above a block of code to parse the result of ComboBox.Name to grab the number as the result would be for example "ComboBox15" I could write a variable assigned to "ListBox" + (ComboBox.Name - "ComboBox") to call ListBox(number) however I'm not sure I can actually then call a variably based name of a listbox.
OK my father helped me come to this solution. It's a little static, but it works perfectly for what I need.
Had to edit the vendor data into multiple sheets and used the Define Name tool under Data to name the range. The listbox changes as per the selection of combobox.
Dim listBoxName As String
listBoxName = "myList" & ComboBox.Tag
Dim rangeName As String
rangeName = "company_1"
Select Case ComboBox.value
Case "Company 1"
rangeName = "company_1"
Case "Company 2"
rangeName = "company_2"
End Select
Dim listBox As Control
Set listBox = Me.Controls(listBoxName)
listBox.RowSource = rangeName
The above code was added to the private sub mentioned in the original thread.

Word VBA: Accessing clicked ActiveX Checkbox

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

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.

Allowing user to select text in word vba macro

In VBA for Word 2007, I want to be able to open a document, highlight sections of text and replace those sections with fields linked to a docvariables. The process would be:
Open document.
Select text.
Select docvariable from list.
Insert field linked to selected docvariable.
Repeat steps 1-4 as required.
There is no way to know beforehand what the text to be selected is or which docvariable is going to be linked to which field or how many times these steps are going to be repeated.
Only with Microsoft could the most absolutely fundamental, simple task of allowing the user to make a selection at run-time and pass this selection back to sub-routine be so tortuous and surreal. I have spent 2 days trying to figure this out. If anyone can help, I will name my next child after you.
I think "tortuous and surreal" is a misconception.
Create a small form with a dropdown (named "selVarName", for example) that lets you select all document variable names available. Link the form to a custom button in the Quick Access Toolbar.
Upon clicking "OK" in this form do something like this:
Private Sub btnOK_Click()
Dim v As Word.Variable
Dim n As String
n = Me.selVarName.Value
With Selection
For Each v In .Document.Variables
If v.Name = n Then v.Delete: Exit For
Next v
.Document.Variables.Add n, .Range.Text
End With
End Sub
And this has bells and whistles already. You can do additional checking like "no text selected", for example.

Renaming Objects in PowerPoint

Probably a very stupid question but I can't figure how to rename an object in PowerPoint.. For example, all my Graphs are called by default "Graph 1" etc.
Could someone help me on that?
Thanks!
In PowerPoint 2007 you can do this from the Selection pane.
To show the Selection pane, click on the Home tab in the ribbon, then click on Arrange and then 'Selection Pane...' at the bottom. The Selection pane will open on the right. (Or press CTRL+F10)
To rename an object, first select the object and then double click on the object name in the Selection pane and you will be able to type the new object name.
(This answer assumes you are merely assigning more meaningful names during development, so your other code that references the objects can be more readable).
Put the code below into a sub, then run it from the slide in question. Each shape will be selected in turn, so you can see what shape is being referenced. An input box will tell you the current name and ask you for a new name. If you cancel or OK a zero-length input, the old name will stay in place. There is no name entry validation in this code, so be sure you type only valid names. After running it once, you can run it again just to check that the names you typed in the first round were applied to the object you intended.
The loop will cover all objects on the current slide, so if you want to process multiple slides, you have to run this separately on each slide. Every object on the slide is considered: title, drawing objects, groups, embedded pictures, equations, etc. etc. - just don't type a new name for objects that you don't care.
After your development is finished, best hide (Private Sub) or erase this code, so your users don't change object names by mistake.
Dim s As Integer, NewName As String
With ActiveWindow.Selection.SlideRange
For s = 1 To .Shapes.Count
.Shapes(s).Select ' So you can see the object in question
NewName = InputBox(.Shapes(s).Name) ' Tell what current name it is and ask for new name
If Len(NewName) > 0 Then .Shapes(s).Name = NewName ' If you typed a new name, apply it
Next s ' 1 To .Shapes.Count
End With ' ActiveWindow.Selection.SlideRange
Thanks for your help but actually I am just doing it using VBA...
ActiveWindow.Selection.ShapeRange(1).Name = "newname"
Cheers