PDA

View Full Version : Userform with Combobox launched from Worksheet



gototcm
03-25-2017, 08:37 PM
Combobox runs under specific setup conditions. This is simple. When the userform is launched a combobox (pull-down list) is shown with 3 items apple, banana, and orange. The user selects one of the three than hits a button to delete one of the items (I've omitted the code for the command button "delete" here for simplicity).

On sheet 1 in cells A1-A3 are the words Apple, Banana, orange respectively I have a userform1 with one combobox (name in property is cbPick). Below is the code for the combobox and Initialize sub. The initialize sub is to populate the combobox with Apple, Banana, and orange.


Private Sub cbPick_Change()
Dim fruitname As String
fruitname = cbPick.Value
End Sub


Private Sub UserForm_Initialize()

cbPick.List = Worksheets("sheet1").Range("A1", Range("A1").End(xlDown)).Value

End Sub

There is one module whose code is

Sub showtheform()

Load userform1
userform1.Show

End Sub

The userform1 is launched from a button on the worksheet "sheet1" and the button is assigned to the Macro showtheform. This works perfectly. However, if I move the button to another worksheet, the userform won't launch and through debugging the code line cbPick.List= . . . . is causing an error 1004. I don't understand why moving the button from one worksheet to another causes the error. I am sorry but I don't know how to upload this simple VBA code.

mana
03-26-2017, 01:05 AM
>cbPick.List = Worksheets("sheet1").Range("A1", Range("A1").End(xlDown)).Value

cbPick.List = Worksheets("sheet1").Range("A1", Worksheets("sheet1").Range("A1").End(xlDown)).Value

gototcm
03-26-2017, 06:19 AM
Thank you for your quick response to my question and I greatly appreciate the help. Your suggestion worked although I don't totally understand the necessity for repeating the "worksheets" object. But the rules are the rules.

mdmackillop
03-26-2017, 07:29 AM
Worksheets("sheet1").Range("A1", Range("A1").End(xlDown)).Value
If you run this from Sheet2 you are attempting to create a list from Sheet1.A1 to bottom of Sheet2 column A.

gototcm
03-26-2017, 06:33 PM
Thanks for your help. Through your and Mana's response I now see the error I was making. I much appreciate your comment.