How to read the selected case in a Combo Box?

actjfc

Active Member
Joined
Jun 28, 2003
Messages
416
Excel Friends, I have the code below, after the user makes the selection, just before running a macro in another module, please, what is the code to read the selection made by the user. I mean the VBA code to read/store in a String Variable the current content of the combo box just before running another macro that need that value as an input. Thanks!

Private Sub UserForm_Initialize()
Dim cLoc As Range
Dim ws As Worksheet
Set ws = Worksheets("EXTENSIONS")

For Each cLoc In ws.Range("EXTList")
With Me.cboLocation
.AddItem cLoc.Value
End With
Next cLoc

Me.cboLocation.Value = "Select"

End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
It is a bit difficult to be specific from what you have said so far.
The value selected is a ComboBox property called Value. In your case this will be cboLocation.Value.

When the ComboBox has been unloaded that property will not be available. So you will need to do something with it before that. For instance, you could have a ComboBox Change Event created that will capture the value and write it to a worksheet cell:

Code:
Private Sub cboLocation_Change()
    Sheet1.Range("B2").Value = cboLocation.Value
End Sub

Or you could add a command button to the userform and make that store the selection somewhere:

Code:
Private Sub CommandButton1_Click()
    CommandButton1.Caption = cboLocation.Value
End Sub

The above code changes the button's caption every time a new value is selected from the combobox.

I hope this helps.
 
Upvote 0
Thanks for your reply. What was actually driving me crazy was the "me." What I did in other module was assigning SelectedLoc = frmName.cboLocation.Value, and it worked. I was using me.cboLocation.Value, and it did not worked. You code may be useful for me also. Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,572
Messages
6,120,306
Members
448,955
Latest member
Dreamz high

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