PDA

View Full Version : VBA code comboboxes (skip blanks & avoid repeats)



pbobadilla
09-12-2012, 06:48 AM
Hello,

I have roughly 50 comboboxes in sheet "TR-Metric Pg2" that pull information off of another sheet called "CTC-Form." I would like to know if there is a way to ignore blank cells so that they do not show up in the combobox drop down menus. Also, is it possible to write VBA code so that you cannot pick the same data twice between all 50 comboboxes? Any help would be appreciated.


Thanks,

Pablo

Tinbendr
09-12-2012, 02:43 PM
Crossposted. (http://www.excelforum.com/excel-programming-vba-macros/859992-vba-combo-boxes-ignore-blanks-avoid-duplicates.html) It is considered rude to post the same question to multiple boards UNLESS you provide a link to the other board.

The first is easy. Just compare to nothing.

If Cells(1,2).value <> "" then
Combobox1.Additem Cells(1,2).value

2. You will have to compare the contents of each combox with the item to add. If there are a lot of items to add, this will bog down after half way.

You could use a Collection to create your unique items and store the range of items for each combobox.

pbobadilla
09-13-2012, 12:48 PM
I apologize about the cross post. It shows I am a newbie :(. I tried placing this code into both the combobox1 code and the code for worksheet "TR-Metric Pg2" and both did not work. Is there something I am doing incorrectly? The range the combo box is gathering data from is BJ11;BJ100 in another worksheet called "CTC-Form". I apologize if I am missing something obvious, but I am just now trying to learn this stuff.

thanks,

Tinbendr
09-14-2012, 02:07 PM
Can you rough out a sample workbook for us. I'm having a little trouble following what you're describing. (And only include about 5 comboboxes. :)

pbobadilla
09-17-2012, 07:24 AM
Can you rough out a sample workbook for us. I'm having a little trouble following what you're describing. (And only include about 5 comboboxes. :)

I have already created the workbook. I am attaching the original workbook and feel free to just look at like 5 comboboxes. It looks like the file is too big and the file type is not supported by this forum. I have uploaded it to the following address sendspace.com/file/30efnk

Thanks,

Pablo

snb
09-17-2012, 07:35 AM
to ignore empty cells:


sub snb
combobox1.list=filter([transpose(if(CTC-Form!BJ11:BJ100="","~",CTC-Form!BJ11:BJ100))],"~",false)
end sub

pbobadilla
09-17-2012, 08:07 AM
Also, this file pulls information from another file in our shared drive through formulas. Therefore, you will get errors int he formula sicne the links are broken.

I set this file up so that our quality department enters quality data into the CTC-Form sheet. They enter information from left to right. Once they enter a quality status for each drum in column A, then a formula in column BJ decides whether it should reference a drum # from column C or to leave it blank. This is done through an IF statement in column BJ for each row of data. Combo boxes in sheet Tr-Metric Pg 2 pull from a range BJ12:BJ10000 in sheet CTC-Form.

pbobadilla
09-17-2012, 08:49 AM
to ignore empty cells:


sub snb
combobox1.list=filter([transpose(if(CTC-Form!BJ11:BJ100="","~",CTC-Form!BJ11:BJ100))],"~",false)
end sub


I tried entering this code into the General code for TR-Metric Pg 2 and it still did not skip blanks for combobox1. I also tried to just assign a button and run the snb macro, but I got an error. What am I doing incorrectly?

Thanks,

snb
09-17-2012, 10:17 AM
Post a sample workbook please.

pbobadilla
09-17-2012, 01:19 PM
Post a sample workbook please.

Here is a sample of what it does with the combo boxes.

snb
09-17-2012, 03:21 PM
There's no code in it ??

Kenneth Hobs
09-17-2012, 08:22 PM
Use single quotes for the sheet name just as you would for standard formulas.

Sub snb()
ComboBox1.List = Filter([Transpose(IF('CTC-Form'!BJ12:BJ21="","~",'CTC-Form'!BJ12:BJ21))], "~", False)
End Sub

Private Sub UserForm_Initialize()
snb
End Sub

snb
09-18-2012, 01:50 AM
1. do not use the property 'listfillrange.
2. remove the values in the property 'listfillrange' in each combobox first.
3. Next step; use:


Private Sub Workbook_Open()
Sheets("Tr-metric Pg2").ComboBox1.List = Filter([Transpose(If('CTC-Form'!BJ12:BJ21="","~",'CTC-Form'!BJ12:BJ21))], "~", False)
for j=2 to 50
Sheets("Tr-metric Pg2").OleObjects("ComboBox" & j).Object.List = Sheets("Tr-metric Pg2").ComboBox1.List
next
End Sub

pbobadilla
09-19-2012, 08:08 AM
1. do not use the property 'listfillrange.
2. remove the values in the property 'listfillrange' in each combobox first.
3. Next step; use:


Private Sub Workbook_Open()
Sheets("Tr-metric Pg2").ComboBox1.List = Filter([Transpose(If('CTC-Form'!BJ12:BJ21="","~",'CTC-Form'!BJ12:BJ21))], "~", False)
for j=2 to 50
Sheets("Tr-metric Pg2").OleObjects("ComboBox" & j).Object.List = Sheets("Tr-metric Pg2").ComboBox1.List
next
End Sub



:(. Still not working for me. I removed all the list fill ranges for each combo box. Then I pasted this code into the general code section of the VBA window for sheet TR-Metric Pg2. What am I doing wrong?

Thanks,

Pablo

snb
09-19-2012, 08:16 AM
Pasting it in the wrong codemodule.

pbobadilla
09-19-2012, 09:19 AM
Pasting it in the wrong codemodule.

I tried pasting this in the "this workbook" code section and when I opened the workbook again it gave me an error saying the following section of the code was not found (highlighted in yellow):

Sheets("TR-Metric Pg2").ComboBox1.List = Filter([Transpose(If( 'CTC-Form'!BJ12:BJ10000="","~",'CTC-Form'!BJ12:BJ10000))], "~", False)

Kenneth Hobs
09-19-2012, 11:21 AM
2. remove the values in the property 'listfillrange' in each combobox first.