fill multiple excel dropdowns in a loop - excel

I have a for loop which creates multiple dropdowns in different parts of my excel sheet. However i am having a hard time filling these with a list. The list should be obtained from the sheets in ws. I guess the problem here is that, when naming my combo box, it doesnt get assigned as an object.
Here is the code i have. Anybody have an idea on how to do this? Thanks in advance.
Private Sub CommandButton1_Click()
Set ws = Sheets(Array("rpi301", "rpi302", "rpi303", "rpi304", "rpi305"))
For Each Sh In ws
Set Cell = Range(Sh.Range("B8").Value & CStr(Sh.Range("B9").Value))
With Cell
ActiveSheet.DropDowns.Add(.Left, .Top, .Width, .Height).Name = "Combo"
ActiveSheet.Shapes("Combo").ControlFormat.ListFillRange = Sh.Range("B2:B7")
End With
Next
End Sub

Code is right. But the problem what naming the dropdowns. They should have unique names.

Related

Clearing a list box and any selected items in list

I have an Excel VBA application that goes through a sheet which contains product orders on a sheet in a workbook and searches the worksheet for orders that match various criteria which is populates in a search worksheet. The contents from this worksheet are then displayed in list box. There are several user forms that allow the user to select an order and then manipulate the order. After doing this the order manipulated may not meet the search criteria so I want to clear the list box contents and the selected row in the list box. I have tried numerous things but nothing seems to work. My latest is something list this:
Private Sub ClearListBox()
UserForm5.lstOpenO.ListIndex = -1
UserForm5.lstOpenO.RowSource = ""
End Sub
But I have tried setting the UserForm5.lstOpenO.Selected to false for all the rows. I have tried clearing the search worksheet and then displaying that which should only show the headers on the columns but the highlight in the selected row remains.
Any help would be greatly appreciated
Bruce
First of all you should not use the default instance of the userform in your code. This will lead to ambigous behaviour
I'd suggest
Private Sub ClearSelectionInListBox()
Dim varItm as variant
With lstOpen0
varItm = .MultiSelect
.MultiSelect = 0
.MultiSelect = 1
.MultiSelect = varItm
End With
End Sub
This will clear the selection within the listbox.
It is not clear if you really want to clear the contents. Because if you want then it does not make sense to think about clearing the selection.
If you want to clear the listbox then it is not neccessary to clear the selection first.
Private ClearListBox()
With lstOpen0
.RowSource = ""
.Clear
End With
End Sub
But after that you need to fill the listbox again.
Further reading
VBA userform
Userform.Show
ListBox

How to distinguish programatically created charts/shapes from all charts/shapes on the worksheet?

I am looking for a way to distinguish between the charts created by my code and the charts created "manually" by the user.
Using chart.name would work if I would set a specific name_prefix or something for the charts but this property is of no use to me as the names can be changed dynamically by the user. I have thought about looking at chart like at shape and change its ID, but no luck with it either.
I have checked chart/shape object model and I cannot find a property, which I could use to somehow distinguish "my charts" from all charts on a sheet collection.
The general idea is that I create a chart with VBA and when user activates it, chart.activate event opens a userform. This userform should open when user activates chart created by my code and not on activating ANY chart on the sheet. I know how to do everything except how to distinguish the charts.
Any ideas how this could be done?
Thanks in advance!
EDIT: I have also thought about adding some information to series names, again, prefix type of info. That would work, but once again - it could be easily changed by the end user and this is what I would like to avoid.
It should be good if you posted the code you use. I (only) can suppose that you activated charts events.
Please, try the next way, which should work with events activated, or not:
Copy the next code in a standard module. It is a Sub which will be assigned to all created charts. It can be used instead of (existing) events, or with events working:
Sub CreatedChart()
Dim ch As Chart
Set ch = ActiveSheet.ChartObjects(Application.Caller).Chart
'you can call the form in discussion here...
Select Case ch.Parent.Name
Case "CrChart1", "CrChart2"
MsgBox "Here you can do something in case of Chart 1 or Chart 2..."
Case "CrChart3"
MsgBox "Here you can do something in case of Chart 3..."
End Select
End Sub
Copy the next code in a module, too. It will create charts and assign the above Sub to them:
Sub testChartsCreate()
Dim ws As Worksheet, ch As ChartObject, i As Long
Set ws = ActiveSheet
For i = 1 To 3
Set ch = ws.ChartObjects.Add(left:=1, _
top:=10, width:=100, height:=100)
ch.Name = "CrChart" & i
ws.Shapes(ch.Name).OnAction = "CreatedChart"
ch.Chart.ChartType = xlLine
'do here all your charts configuration...
Next i
End Sub
You can identify which of all existing charts on a sheet have been created by the above code. Please, take care to also have some manually created charts, or programmatically, but not by the above code (type), which assigns that specific Sub:
Sub testIdentifCrCharts()
Dim sh As Worksheet, ch As ChartObject, i As Long
Set sh = ActiveSheet ' use here the necessary sheet
For Each ch In sh.ChartObjects
Debug.Print ch.Name, isCreatedChart(ch.Chart, sh)
Next
End Sub
Private Function isCreatedChart(ch As Chart, sh As Worksheet) As Boolean
If sh.Shapes(ch.Parent.Name).OnAction = "CreatedChart" Then
isCreatedChart = True
End If
End Function
I used ch As Chart like the first function parameter, for the case of Charts to be checked, not ChartObjects...
The above solution may look complicated, but it is very easy to be understood and applied, in fact.
Please, test the above suggestion and send some feedback.
One potential idea would be to put your magic value in MailEnvelope.Introduction. This is a VBA-settable string property you likely won't ever use for its intended purpose, and it isn't exposed in the UI for a user-created chart.

Detect ComboBox Change Excel VBA

I have a sheet with a bunch of ComboBoxes(form control) and I want to detect when a user changes any one of them and write text in a cell. Using Worksheet_Change on the target cells doesn't work. I have tried a bunch of things that don't work. I'm not sure what needs to be in the private sub line or the if statement.
Private Sub DropDowns_DropButtonClick()
If ActiveSheet.DropDowns.Value > 1 Then
Cells(13, 5).Font.Bold = True
Cells(13, 5).Font.Color = vbRed
Cells(13, 5).Value = "!!! Selections have been changed. !!!"
End If
End Sub
I have tried
ComboBox_AfterUpdate()
ComboBox_Change()
DropDowns_AfterUpdate()
DropsDowns_Change()
and anything else I could find. I've also tried a few different things in the if statement with no luck.
I appreciate any help.
Chris
If I'm reading you correctly, you're comboboxes are in a userform. If I'm correct, simply open your userform in 'Visual Basic' and double click on the relavant combobox. This will open the code pane and create an empty Private Sub routine called 'Private Sub <Combobox Name> ()'.
Enter your code to place your data in the sheet (or whatever else you want) into the subroutine and Bob should be your uncle.
Apologies in advance if there's something I've missed.
RannochRob
Edit...
OK, my mistake, it's a form control.
My first comment is that it's easier to use an activex control if you can... however, with a form control, should (a) Use the cell link box in the 'Format Control' drop down ('Control' tab) to place the result in a cell... however, that result will not be the content of the box but an integer equal to the position of the selected entry on the list of entries in the combobox. You then need to (b) assign a macro to the combobox which will pick up the result and use it to get the required information from the range containing the list of entries. Like I say, much easier with an activex control...
RannochRob
Here's how you can do it by assigning a macro to the combobox (right click on the combobox>assign macro) as #BigBen mentioned in the comments section:
Option Explicit
Sub DropDown1_Change()
Dim sht As Worksheet
Dim dd As DropDown
Set sht = ThisWorkbook.Worksheets("Name of your Worksheet") 'Name of the worksheet in which the combobox is located
Set dd = sht.DropDowns("Drop Down 1") 'name of your combobox
sht.Range("G1").Value = "The selected value is: " & dd.List(dd.Value) 'dd.value returns the index of the selected value
End Sub
You can use the same code for each one of your comboboxes.
For demonstration purposes i have used the following set-up:
You can easily modify the code to best fit your needs.

Auto populate Combobox from range

I am using Excel 2013 and I have written the below code to auto populate a combobox from a range for one of my userforms. I have the combobox named "Times"
Sometimes this populates the list and other times it gives a blank list even though the range did not change.
Any idea how to stop this and just have it work everytime?
Private Sub UserForm_Initialize()
Sheets("Backend").Visible = xlSheetVisible
Times.RowSource = Sheets("Backend").Range("E1:E96").Address
Sheets("Backend").Visible = xlVeryHidden
End Sub
I have found a different way to do this now that works. Thanks to this post.
Populate list box with a named range

Stumped: ExcelVBA RowSource for a ComboBox

I'm stumped. I've read every thread regarding applying a RowSource to a ComboBox and just cannot get the solutions (which are mostly the same) to work.
The details are:
Excel 2010
UserForm = UFToTakeList
ComboBox = cbLvl2Selector
For which I want to have the RowSource as NRM1Lookup!NRM1GroupNoRange
I have typed: UFToTakeList.cbLvl2Selector.RowSource = NRM1Lookup!NRM1GroupNoRange to no success. I have added "" and '', I have tried other variations on the theme.
I can type the RowSource in the Properties box but I want to be able to concatenate another Range and know that I can apply this method to all my ComboBoxes.
I think my fundamental problem is knowing where to put the code. It automatically selects the _Change parameter for the ComboBox within the UserForm. If that is correct then I cannot workout why I can type the path above into the Properties box but it won't work as code.
Any guidance would be very much appreciated. My grey matter is frying.
From your explanation I could not understand what you want? You can try following codes, is it helpful to you or not?
Private Sub UserForm_Initialize()
Dim rng As Variant
rng = Sheets("Sheet1").Range("A1", Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp))
If IsArray(rng) Then
ComboBox1.List = rng
Else
ComboBox1.AddItem rng
End If
End Sub

Resources