Setting Combobox to a selected item

svjack9

New Member
Joined
Feb 9, 2014
Messages
32
I have a userform that is populated from a worksheet containing saved chemical application records including date applied, site_Id, quantity applied, etc. The form contains a combobox ("cboChemicals") which is populated by a second worksheet "Chemicals". The Chemicals worksheet contains all the chemicals we use and the EPA registration number. When I retrieve a application record and populate the form it will contain, among other things, the name of the chemical used in the application. I want to set me.cboChemicals to the row in its list that equals the chemical used. Here is the code segment:
dim wb as workbook
dim ws_Chemicals as worksheet, rng as range
dim RowNum as integer

set wb = thisworkbook
set ws_Chemicals = wb.worksheets("Chemicals")
set rng = ws_Chemicals.Range("A1:A500").find(what:=Chem_Name,lookat:=xlwhole)
if not rng is nothing then
RowNum=rng.Row
me.cboChemicals=ws_Chemicals.cells(RowNum,1)
endif
********************************
Even though the combobox may display the name of the chemical, I know it isn't set to the row because cboChemicals.column(1) should return the EPA number and it doesn't. I've tried ListIndex and some of the other properties without success. I could populate the combobox with just the selected chemical and its EPA Number, but I would prefer to set the index to the row of the selected chemical. (I'm not sure I'm using the right terminology so I hope you can decifer what I'm trying to say.) Any suggestions would be greatly appreciated!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try this - which assumes that the combobox is populated from the same range

VBA Code:
    Set wb = ThisWorkbook
    Set ws_Chemicals = wb.Worksheets("Chemicals")
    Set Rng = ws_Chemicals.Range("A1:A500").Find(what:=Chem_Name, lookat:=xlWhole)
    If Not Rng Is Nothing Then Me.cboChemicals.Value = cboChemicals.List(Rng.Row - 1)
 
Upvote 0
Thanks for that. I've been struggling with this issue for a long time. I should have posted sooner!
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,391
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top