0% found this document useful (0 votes)
96 views4 pages

Vba - Excel ActiveX Combo Box Not Showing Selection - Stack Overflow

The document is a question posted on Stack Overflow about an issue with an ActiveX combo box in Excel VBA. The user is trying to filter the combo box options based on checked checkboxes, but when an option is selected it does not show in the combo box. Another user responds suggesting moving the filtering code to a macro assigned to the checkboxes rather than using the combo box events. They provide sample code to load the combo box options based on checked checkboxes.

Uploaded by

angbohk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
96 views4 pages

Vba - Excel ActiveX Combo Box Not Showing Selection - Stack Overflow

The document is a question posted on Stack Overflow about an issue with an ActiveX combo box in Excel VBA. The user is trying to filter the combo box options based on checked checkboxes, but when an option is selected it does not show in the combo box. Another user responds suggesting moving the filtering code to a macro assigned to the checkboxes rather than using the combo box events. They provide sample code to load the combo box options based on checked checkboxes.

Uploaded by

angbohk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

10/21/22, 10:47 AM vba - Excel ActiveX Combo Box not Showing Selection - Stack Overflow

Excel ActiveX Combo Box not Showing Selection


Asked
1 year, 1 month ago Modified
1 year, 1 month ago Viewed
304 times

I'm using a ActiveX Combo Box to show all or some Worksheets. In addition to that, on the
same worksheet I have some Form Controls Check Boxes where the user can use them as
0 filters for the Combo Box. So for instance, each check box have the name of department and
so when one is checked the list will be updated with the worksheets related to that name.
Which works fine.

However, the problem I have is that if I do choose an option from the Combo Box drop down
list, it doesn't come up on the field of the Combo Box.

Here is the code I'm using at the moment.

Private Sub TransferList_DropButtonClick()

Application.EnableEvents = False

Dim ws As Worksheet

I = 1

TransferList.Clear

For Each ws In Sheets

If ActiveSheet.Shapes("CheckBox_Viva").ControlFormat.Value = 1 Then

TransferList.AddItem ws.Name

I = I + 1

End If

Next ws

Application.EnableEvents = True

End Sub

I did some research and I did find that by using the TransferList_Change the issue is resolved
but the filtering is not working (no change whether a check box is True or False ).

What am I missing?

Cheers.

excel vba combobox

Share Follow asked Sep 14, 2021 at 10:25


Geo Koro
75 6

Btw, I just started writing the code for the "filtering" so, that is just a first attempt for me to see how it's
working. The reason I used 'TransferList_DropButtonClick' is because the number of worksheets changes
Join StackasOverflow
well as their
to names.
find theIf you
besthave any alternative,
answer smarterquestion,
to your technical way pls feel freeothers
help to share.
–  Geo Koro
Sep
Sign up
14,
answer theirs.2021 at 10:26

https://stackoverflow.com/questions/69176062/excel-activex-combo-box-not-showing-selection 1/4
10/21/22, 10:47 AM vba - Excel ActiveX Combo Box not Showing Selection - Stack Overflow

Please, try using the check boxes assigned macro to load the combo. The combo events should be used
for something else...
– FaneDuru
Sep 14, 2021 at 11:15

Hey FaneDuru, not sure what am I suppose to do. I'm not experienced in coding... Could you pls be
more explicit?
–  Geo Koro
Sep 14, 2021 at 11:23

So, You should assign a macro (or more) to the check boxes based on which value the combo should be
loaded/filtered. And the combo should be loaded when such a check box will be changed. Now you
said that the check boxes are on the same sheet and I suppose that you do not have all of them
named identic. You should move the code you placed in the combo event, in that sub. And taking care
to use all check boxes I (only) suppose that have to be taken in consideration for a specific loading. You
can know what combo was clicked using Application.Caller , if assign the same Sub to all...
– FaneDuru
Sep 14, 2021 at 11:31

If I should know what you want accomplishing (from the words part...) I can help with a piece of code,
too. But I must confess I do not know what you try doing...
– FaneDuru
Sep 14, 2021 at 11:32

Sorted by:
1 Answer
Highest score (default)

Like I said in my comment I will leave in some minutes. Please, try understanding the next way
of working and extrapolate it at your situation. If something unclear, do not hesitate to ask.
1 But I will be able to answer only after some hours, when I will be at home.

1. Open a new workbook and save it as 'xlxm`, to accept macros.

2. Place on the working sheet a combo box (ActiveX type) and so many Form Type check
boxes as workbook number of sheets. Name them (name and caption) exactly as the
sheets, or in a way to make them matching one or some more sheets. Name the combo
as "TransferList".

3. Copy the next code in a Standard module:

Sub LoadSheets_Combo()

Dim ws As Worksheet, cmb As MSForms.ComboBox

Set cmb = ActiveSheet.OLEObjects("TransferList").Object

cmb.Clear

For Each ws In Sheets

If ActiveSheet.Shapes(ws.Name).ControlFormat.Value = 1 Then

cmb.AddItem ws.Name

End If

Next

End Sub

4. Right click on each check box and choose Assign macro... and choose 'Maros in: This
workbook and at 'Macro name' choose LoadSheets_Combo`.

5. Start paying with check box values and see how the combo is loaded, only with the sheets
matching (somehow) with the ticked check boxes.

Test the above suggested scenario and send some feedback...

Join Stack Overflow


Edited : to find the best answer to your technical question, help others
Sign up
answer theirs.

https://stackoverflow.com/questions/69176062/excel-activex-combo-box-not-showing-selection 2/4
10/21/22, 10:47 AM vba - Excel ActiveX Combo Box not Showing Selection - Stack Overflow

Please, try the next code able to do what (I understood) you need for your case:

Option Explicit

Sub LoadSheets_Combo()

Dim ws As Worksheet, cmb As MSForms.ComboBox, strDep As String, strProd As String,


arrDep, arrProd

Dim chB As CheckBox, iD As Long, iP As Long, mtch, arrL(), boolAllFalse As Boolean

'ReDim the arrays keeping departments and products at their maximum possible size:

ReDim arrDep(ActiveSheet.CheckBoxes.Count - 1): ReDim


arrProd(ActiveSheet.CheckBoxes.Count - 1):

For Each chB In ActiveSheet.CheckBoxes 'iterate between check boxes:

If Mid(chB.Name, 9, 2) = "De" Then 'if a check box refers a department


name:

If chB.Value = 1 Then 'if its value is True:

arrDep(iD) = chB.Name: iD = iD + 1 'put it in the departments array

End If

End If

If Mid(chB.Name, 9, 2) = "Pr" Then 'if a check box refers a product name:

If chB.Value = 1 Then 'if its value is True:


arrProd(iP) = chB.Name: iP = iP + 1 'put it in the products array

End If

End If

Next

If iD > 0 Then ReDim Preserve arrDep(iD - 1) 'redim the array preserving only the
loaded elements

If iP > 0 Then ReDim Preserve arrProd(iP - 1) 'redim the array preserving only the
loaded elements

Set cmb = ActiveSheet.OLEObjects("TransferList").Object 'set the combo to be


loaded

cmb.Clear 'clear the combo items

boolAllFalse = onlyFalseChkB 'check if all check boxes value is False and place
the result in a boolean var

For Each ws In Sheets 'iterate between all sehets

If boolAllFalse Then 'if all checkboxes value are


False:

cmb.AddItem ws.Name 'add the sheet name in the combo

Else 'if not all check boxes


value are False:

If iD > 0 Then 'if there are department


check boxes in departments array:

mtch = Application.Match("CheckBox" & Mid(ws.Name, 9, 3), arrDep, 0)


'check if the sheet is found in the array

If Not IsError(mtch) Then 'if found

If cmb.ListCount > 0 Then 'if there are items in the combo

arrL = cmb.List 'extract the combo items in


an array a 2D array with 10 columns (fastest way)

ReDim Preserve arrL(0 To cmb.ListCount - 1, 0 To 0) 'replace all


(Null) values from columns 1 to 10)

mtch = Application.Match(ws.Name, arrL, 0) 'check if


the sheet name is already added in the combo

If IsError(mtch) Then 'if not added:

cmb.AddItem ws.Name 'add it

End If

Else

cmb.AddItem ws.Name 'add the sheet name in the combo,


if combo does not have any item (yet)

End If

End If

End If

'check products chkB:

Join Stack OverflowIftoiP > 0 Then


find 'proceed in the same way for
the best answer to your technical question, help others
the products check boxes array:
Sign up
answer theirs. mtch = Application.Match("CheckBox" & Right(ws.Name, 3), arrProd, 0)

https://stackoverflow.com/questions/69176062/excel-activex-combo-box-not-showing-selection 3/4
10/21/22, 10:47 AM vba - Excel ActiveX Combo Box not Showing Selection - Stack Overflow
If Not IsError(mtch) Then

If cmb.ListCount > 0 Then

arrL = cmb.List

ReDim Preserve arrL(0 To cmb.ListCount - 1, 0 To 0)

mtch = Application.Match(ws.Name, arrL, 0)

If IsError(mtch) Then

cmb.AddItem ws.Name

End If

Else

cmb.AddItem ws.Name

End If

End If

End If

End If

Next

End Sub

Function onlyFalseChkB() As Boolean

Dim chB As CheckBox

For Each chB In ActiveSheet.CheckBoxes

If chB.Value = 1 Then Exit Function

Next

onlyFalseChkB = True

End Function

In order to load the combo according to the above Sub rules when the sheet is activated,
please copy the next code event in the sheet keeping the controls code module:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

LoadSheets_Combo

End Sub

Share Follow edited Sep 17, 2021 at 11:18 answered Sep 14, 2021 at 13:26
FaneDuru
32.8k 4 17 24

Thanks for that. Haven’t had the chance to check it as yet. I’ll get back to you later
–  Geo Koro
Sep 15,
2021 at 3:10

I'm getting a runtime error '-2147467259 (80004005)' on 'cmb.Clear'


–  Geo Koro
Sep 15, 2021 at 10:54

@Geo Koro Are there in the active sheet any ActiveX combo box named "TransferList"? I created a test
workbook and it works as it should. Please, download it from here and see it working. Just play with the
three check boxes value and check what is loaded in the combo...
– FaneDuru
Sep 15, 2021 at 16:11

Thank you sooooo much for your time. Is working! Not sure what I did wrong before but I started a new
workbook and it does work. Now I need to work how I'm going to apply multiple filters.
–  Geo Koro
Sep 16, 2021 at 3:09

Please, test the new workbook downloading it from here and send some feedback. I downloaded the
previous one on somebody else laptop and transformed it according to my understanding. Please,
confirm that my understanding was correct...
– FaneDuru
Sep 16, 2021 at 6:42

Join Stack Overflow to find the best answer to your technical question, help others
Sign up
answer theirs.

https://stackoverflow.com/questions/69176062/excel-activex-combo-box-not-showing-selection 4/4

You might also like