Populate 10 comboboxes from the same array using VBA in MS Word - vba

I want to create one and the same array to be used for my 10 comboboxes. When the OnClick procedure is activated for a ComboBox, the combobox will be filled with the array.I am no programmer but I use simple VBA to solve some problems. Now I have encountered problems and have no idea how to solve it. Previously, I have only used Excel and linked cell. Now it's Word ...
I want all tests to be visable in the first column of my combobox and the colors in the second column.
My array
Test1, Blue
Test2, Green
Test3, Yellow
Test4, Black
I have tried to google around but feel I can not put everything together.
Private Sub Document_Open()
' The location of my array?
End Sub
Private Sub ComboBox1_Click()
' Load object with array, code for each combobox?
End Sub
I'm trying to create a simple order form. I use ActiveX components and document protection.

Use the DropButtonClick event of the ComboBox to perform actions when the user clicks the dropdown button of a ComboBox control.
Since you want to use the same code for all your ComboBoxes, create a single procedure for populating the list and pass the ComboBox object to it.
To me, it seems simplest to store the list as a delimited string, then use the Split function to break that up. First, the items are split into an array. Then loop through that array and split again to get the two entries for a list item, using these to populate a third array that will be assigned to the ComboBox, using the List method.
Private Sub ComboBox1_DropButtonClick()
PopulateList ComboBox1
End Sub
Private Sub ComboBox2_DropButtonClick()
PopulateList ComboBox2
End Sub
Sub PopulateList(cb As MSForms.ComboBox)
Dim sListVals As String
Dim aList As Variant, aCols As Variant, aItems As Variant
Dim i As Long
'cb.Clear
sListVals = "Test1,Blue;Test2,Green;Test3,Yellow;Test4,Black"
aList = Split(sListVals, ";")
ReDim aItems(UBound(aList), 1)
For i = LBound(aList) To UBound(aList)
aCols = Split(aList(i), ",")
aItems(i, 0) = aCols(0)
aItems(i, 1) = aCols(1)
Next
cb.List = aItems
End Sub

Related

Question on multiple checkboxes launching code

I have a user form and a frame with 35 checkboxes in it, numbered 1 to 35. They represent 35 Named Ranges. I test to see if any of the name ranges are not set, if set correctly the checkbox value is set to TRUE.
I found some code that would allow me to trigger a sub if one of the checkboxes is clicked. That code seems to work, but my check code above also triggers the checkbox events, which I do not want. I only want the sub to run when the checkbox is clicked with the mouse? I can post the code I'm using, but though I'd first ask the question to see if what I would like to do is possible.
Thanks,
Jim
Code in class module:
Public WithEvents ChkBox As MSForms.CheckBox
Public Sub AssignClicks(ctrl As Control)
Set ChkBox = ctrl
End Sub
Public Sub chkBox_Click()
If chkBoxProcess = "Y" Then
'ThisWorkbook.Worksheets("Sheet1").Range(ChkBox.Name).Value = Format(Now, "dd.mm.yyyy")
'MsgBox ("check box number = " & ChkBox.Name & " " & ChkBox.Value)
' Else
End If
End Sub
Code in Forms:
Public Sub UserForm_Initialize()
Dim SheetCount, i As Integer
Dim sh As Worksheet
'Public SheetName, SheetName2, StartOldNewTimeA, OldNewTimeAdd As String
'Initialize the form frmChgNameRng
'Set array values of the day options
'Set array values for 12:00 timeframes
'Set array values for 12:30 timeframes
'Set colors used in Checkboxes
'Set array for Checkboxes (boxes are numbered accross the page, 1 corressponds to Mon_1200/Mon_1230, 8 corresponds to Mon_200/Mon_230, etc.)
'Formulas are placed in the time cells on the left of the page, the macro will add the appropriate value into the Mon_1200 time slot and all other cells update off that cell
chkBoxProcess = "N"
Dim ChkBoxes As cls_ChkBox
Dim ctrl As Control
Set colTickBoxes = New Collection
For Each ctrl In Me.Controls
If TypeName(ctrl) = "CheckBox" Then
Set ChkBoxes = New cls_ChkBox
ChkBoxes.AssignClicks ctrl
colTickBoxes.Add ChkBoxes
End If
Next ctrl
'..... lots of code for Range Name Checks, etc.
End Sub
Your code is conflating control state with model data, and so the only way to tell it "named range 32 is ON", or "named range 13 is OFF", is to alter a checkbox' state, which fires that control's Change event.
There's no way around that, it's just how controls work: they fire a Change event whenever their value changes, regardless of how that's done.
Instead of having controls' state be the data, make the controls' state alter the data.
This requires conceptualizing this data, first: looks like you need to associate a number/index to some Boolean value. An array can do this.
Private namedRangeStates(1 To 35) As Boolean
Note that depending on what you're doing, initializing the state should be feasible by iterating the workbook's Names collection in the UserForm_Initialize handler. Or better, the form could expose a method that takes an array of Boolean values, and copies that state into namedRangeStates.
Now, when a checkbox is modified, make it alter the state:
Private Sub Checkbox31_Change()
namedRangeStates(31) = Checkbox31.Value
End Sub
Your form can expose that state as a property:
Public Property Get NamedRangeState(ByVal index As Long) As Boolean
NamedRangeState = namedRangeStates(index)
End Property
Public Property Let NamedRangeState(ByVal index As Long, ByVal value As Boolean)
namedRangeStates(index) = value
End Property
And now you can modify the enapsulated state independently of the combobox values.

VBA: ComboBox only showing one item after Workbook_Open event

I am attempting to have a Workbook_Open event populate a controls ComboBox
so that when the user goes to the Worksheet("Benchmarking"), they have a pre-populated list to choose from that includes all the items in the array datesArr.
The problem i am having is, upon opening the spreadsheet and navigating to the Worksheet("Benchmarking"), i am only seeing one item in the drop down list:
If i select that item then the list actually populates:
Desired result:
I want the full list to be available from the first time the user tries to make a selection not just after the ComboBox1_Change event is fired.
Having reviewed numerous post e.g. Sometimes the ActiveX Combobox only shows one row, why? , Populating Combo Box on WorkBook Open I have tried several different approaches including the following in the Workbook_Open event code:
.ListFillRange = "DropDownDates"
.List = DateArrToStrAr
I have also looped the array adding the items to ComboBox1. Each time i get the same 1 visible item in drop down result.
Is someone able to tell me where i am going wrong please?
My current code is
1) ThisWorkbook
Private Sub Workbook_Open()
With Worksheets("Benchmarking").OLEObjects("ComboBox1").Object
.Clear
.List = DateArrToStrArr '
End With
End Sub
2) Worksheet("Benchmarking"):
Private Sub ComboBox1_Change() 'QH 2/11/17
Dim datesArr() As String
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Lkup")
datesArr = DateArrToStrArr 'function that reads a named range of dates and converts to string to avoid dd/mm becoming mm/dd
If ComboBox1.Value = vbNullString Then ComboBox1.Value = "01/04/2016"
ComboBox1.List = datesArr
'.....other code
End Sub
Notes:
The array datesArr is populated by the function DateArrToStrArr() which reads in a named range of dates "DropDownDates" (workbook scope) and converts them to a string array. This is then assigned to the ComboBox.
DropDownDates is a dynamic named range with formula =OFFSET(Lkup!$F$16,,,Lkup!$M$12,)
Set-up: Excel 2016 64 bit Windows.
Thanks to #CLR for making me think about recalcs. I decided to hack my way around this with the following:
I have added in Worksheet("Benchmarking") a Worksheet_Activate event and removed the Workbook_Open code. This seems to do the trick
Private Sub Worksheet_Activate()
' ComboBox1.Clear
ComboBox1.List = DateArrToStrArr
End Sub

Passing Cell Values into a combo box list

I am looking to pass cells values into the list for a combo box in Excel.
I have created an array to pass the cell values into but they aren't getting to the combo box list when they are being called. I need the cell values to display as the drop down items and all that displays is the blank space:
Sub Clear_Invoice_Data()
Dim Arr As Variant
Arr = Range("A1:A5").Value
frmAddLineItem.ddlProduct.List = Arr
End Sub
It is called by a button from a User form:
Private Sub cmdClearAll_Click()
Call Button_Functions.Clear_Invoice_Data
i = 18
End Sub
Pictures of problem: (no values)
There is another (rougher) method found here to do this if you so choose. Also make sure you define the Sheet name you are sourcing the data from:
Sub AddItemsToBox()
Dim itemcell As Range
For Each itemcell in Sheets("Source Sheet Name").Range("A1:A5")
frmAddLineItem.ddlProduct.AddItem itemcell.Value
Next itemcell
End Sub
Using ComboBox, you can use .List property of the same to populate worksheet values to combox box.
Check this below example:
ComboBox1.List = Worksheets("Sheet1").Range("A1:A5").Value
If you want to use Array,
Dim Arr As Variant
Arr = Worksheets("Sheet1").Range("A1:A5").Value
ComboBox1.List = Arr
I am just getting into VBA and I am doing exactly this. I think the way I found is pretty easy and straightforward. I'll share it in case it can help someone
Declare variable to store table from Worksheet as "range". Make sure
it's outside of sub routines so it can be called by any function
Dim myTable As Range
Store table values in declared variable. Then select which column (range) of values you want to store in your combo box.
Private Sub UserForm_Initialize()
Set myTable = Worksheets("dataSheetName").Range("A2:B6")
Me.myComboBox.List = myTable.Columns(1).Value
End Sub ```

lAdding files to a two column listbox

I am trying to add files to a list box in the same way as they are added when files are attached to an email, in two columns but able to address each file uniquely.
I am using Access VBA and do not know if it is possible.
Any help would be appreciated.
Thanks
Try mapping the indices of the items in the listBox to the files using a separate array/collection/dictionary. Then, when an item is selected, you can use the item's index to get the link
I just tried this below, works perfectly.
Userform Code:
Private d As Dictionary
Private Sub userform_initialize()
Set d = New Dictionary
'populate list box, and add items to dictionary
For i = 1 To 3
With ListBox1
d.Add .ListCount, "Link" & i
.AddItem 0
.List(.ListCount - 1) = "Hello World"
End With
Next
End Sub
'update label1's value based of listbox's selected item
Private Sub ListBox1_Change()
With ListBox1
If .ListIndex <> -1 Then
Label1.Caption = d.Item(.ListIndex)
End If
End With
End Sub
And the userform looked like this: (listbox named ListBox1, and label named Label1):
Example:
If you have problems using the dictionary object, you need to add a reference to "Microsoft Scripting Runtime". But really any collection-type can replace the dictionary in this case (an array/Collection)

How do I refer to a controls object, on a worksheet, using a variable name?

I have added a ListBox to a SHEET (not to a "UserForm")
I did this using the mouse.
I clicked the little Hammer and Wrench icon.
This ListBox seems to be easily referenced using code such as this:
ListBox1.Clear
or
ListBox1.AddItem("An option")
However, I have three of these ListBoxes (named, conveniently, ListBox1, ListBox2, and ListBox3) and I want to write a function to populate them with array data, like this:
Call populate_listbox(ListBox2, designAreaArray)
Where the first argument is the listbox name, the 2nd is the data.
But I do not know how to send "ListBox2" correctly, or refer to it correctly within the function.
For example:
Dim controlName as string
controlName = "ListBox1"
doesn't work, even if I define the function as follows:
Sub populate_listbox(LB As ListBox, dataArray As Variant)
Dim i As Integer: i = 0
For i = LBound(dataArray, 2) + 1 To UBound(dataArray, 2) ' Skip header row
LB.AddItem (dataArray(index, i))
Next i
End Sub
Clearly it results in a mis-matched data type error. I've tried defining "controlName" as a ListBox, but that didn't work either...
Though perhaps it is my reference to the listBox that is incorrect. I've seen SO MANY ways to refer to a control object...
MSForms.ListBox.
ME.ListBox
Forms.Controls.
Worksheet.Shapes.
The list goes on an on, and nothing has worked for me.
Try this:
Dim cMyListbox As MSForms.ListBox
Set cMyListbox = Sheet1.ListBox1 '// OR Worksheets("YourSheetName").Listbox1
cMyListbox.AddItem("An option")
Also you can populate a listbox without having to loop through the array, try this:
Dim cMyListbox As MSForms.ListBox
Dim vArray As Variant
Set cMyListbox = Sheet1.ListBox1
vArray = Range("A1:A6").Value
cMyListbox.List = vArray
Change the sub signature to match this:
Sub populate_listbox(LB As MSForms.ListBox, dataArray As Variant)
Now you can pass it like you were trying to originally.
NOTE: This only works if you used the "ActiveX" version of the listbox. I'm assuming you are because you are able to call ListBox1 straight from a module.
PS: The ActiveX controls are members off of the parent sheet object. So if you have the listbox1 on sheet1, you can also call it like Sheet1.ListBox1 so you don't get confused if you end up with multiple sheets with multiple listboxes. Also, you may want to change the name just to make it easier on yourself.
Dim controlName As OLEObject
Set controlName = Sheet1.OLEObjects("ListBox1")
Call populate_listbox(controlName, designAreaArray)
Sub populate_listbox(LB As OLEObject, dataArray As Variant)
Dim i As Integer: i = 0
For i = LBound(dataArray, 2) + 1 To UBound(dataArray, 2) ' Skip header row
LB.Object.AddItem (dataArray(Index, i))
Next i
End Sub
To access the state of a checkbox Active-X control on Sheet1:
Dim checkBox1 As Object
Set checkBox1 = Sheet1.OLEObjects("CheckBox1").Object
MsgBox checkBox1.Value