select from list in VBA

ajm

Well-known Member
Joined
Feb 5, 2003
Messages
2,053
Office Version
  1. 365
Platform
  1. Windows
how do you create a drop down list for the user to select from in vba? In the code I have, the user has been manually typing in the name of the Key Account Manager:

Code:
KAM = InputBox("Enter Key Account Manager's Name:", "Name Required")

I want to remove any margin for error by having them select from a list instead. My named range with the list of names in is called "Key_Account_Manager"
 
Ok bud,

I just made a combo box example for you that displays 2 methods to assign a value from a c box to a variable. Be warned...... It is commented to hell, lol. Just try to look through it though. I tried to explain EVERY little step I took. There is only code in the form object and in the 'ThisWorkbook' object so it should make it easy to locate.

Here is the file: http://www.mediafire.com/?edt9tkvatol

And oh yeah, to answer you last question.... Yes, declaring the variable as a Variant COULD be causing you some issues, but it seems unlikely. There is probably something you are just not noticing.

Hope this helps!

Chad
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
thanks CHad. essentially, it looks like you have to pass the value to a cell on a worksheet first, and then assign the variable. does that sound about right? if so, any ideas why it can't just be assigned to a variable straight away?
 
Upvote 0
nah, you don't have to assign it to a cell first, I was just showing you how you could then pass the varible to a cell if so desired. The first thing I did for both events was pass the value of the combo boxes to the variable:

Code:
Private Sub cmbTest_Change()

KAM = cmbTest.Text '< Assign text from the c box to KAM

Worksheets(1).Cells(2, 4).Value = KAM '< Assign value of KAM to the cell

Call MsgBox("KAM = " & KAM, vbOKOnly, "What is KAM?")

end sub

Like I said the line 'KAM = cmbTest.Text' is where you're passing the info to the variable. The other two lines are just for show and debugging so you could see that KAM was indeed holding the proper value.

Chad
 
Upvote 0
i am still having trouble. i can assign the value to the cell but don't get anything in the message box. I have moved the declaration of the variable to
ThisWorkbook (as you had it); I have personalised the code (in the userform) to:
Code:
Private Sub CommandButton1_Click()

'check for a Key Account Manager Name
If Trim(Me.txtKeyAccountManager.Value) = "" Then
  Me.txtKeyAccountManager.SetFocus
  MsgBox "Please enter a Key Account Manager Name"
  Exit Sub
End If
KAM = Me.txtKeyAccountManager.Value
Worksheets("Lists - Product, KAMs etc").Range("H2").Value = KAM


cmbKAM.Hide
End Sub

and finally, in the sheet itself:
Code:
Sub test()
cmbKAM.Show


Call MsgBox(KAM)

End Sub

with the message box in the userform itself, it shows up the proper result. in both your example and mine variation, the value gets properly passed to the worksheet but not between modules.

any further clues?
 
Upvote 0
Is Sub Test in a Sheet Module or a normal module?
I'd put it in a normal module. And check the the declaration to make sure its Public
Code:
Public KAM as Variant: Rem or String
 
Upvote 0
mike, just responded to you on VBAX. Sub Test is in a sheet at the moment as the routine this will go into is in a sheet. i guess it could just as easily go into a module and get called from the sheet? wouldn't i still face the problem of how to get the variable KAM recognised in the sheet though?
 
Upvote 0
I'd declare KAM in the normal module.
As a test, this in a sheet module
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
kam = "x"
Call test
MsgBox kam
End Sub
and this in a normal module
Code:
Public kam As String

Sub test()
kam = "smith"
End Sub
produced "smith".
Moving the declaration to the sheet module,
Code:
Public kam as String

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
kam = "x"
Call test
MsgBox kam
End Sub
Code:
Sub test()
kam = "smith"
End Sub
produced "x"
 
Upvote 0
and there we have it. Thanks mike. The declaration must go into the Module and not ThisWorkbook, nor the sheet.

thanks Chad for your help also.
 
Upvote 0
one last question on this one Mike. How do I ensure the form is Unloaded before use each time, so that when the user .shows the form, it pops up clean?
 
Upvote 0

Forum statistics

Threads
1,223,103
Messages
6,170,123
Members
452,303
Latest member
c4cstore

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