Consulting

Results 1 to 5 of 5

Thread: Userform with Combobox launched from Worksheet

  1. #1
    VBAX Regular
    Joined
    Dec 2011
    Posts
    16
    Location

    Userform with Combobox launched from Worksheet

    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.

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    >cbPick.List = Worksheets("sheet1").Range("A1", Range("A1").End(xlDown)).Value

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

  3. #3
    VBAX Regular
    Joined
    Dec 2011
    Posts
    16
    Location
    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.

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Regular
    Joined
    Dec 2011
    Posts
    16
    Location
    Thanks for your help. Through your and Mana's response I now see the error I was making. I much appreciate your comment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •