+ Reply to Thread
Results 1 to 2 of 2

Pass variable from user form to macro

  1. #1
    r wilcox
    Guest

    Pass variable from user form to macro

    I'm using a user form to get a selection from a customer, which I then will
    place in a cell in the worksheet. How do I pass this selection from the user
    form to the controlling sub?

    Sub PopulateListWithShingleTypeRange()
    'Shingle Type Selection
    Dim Selection As String
    Dim ShingleSeletion As String
    For Each x In Sheet1.Range("StblShingleType")
    UserForm1.ListBox1.AddItem x.Value
    Next
    UserForm1.Caption = "Shingle Selection"
    With UserForm1.Label1
    ' Set the text of the label.
    .Caption = "Select the Shingle Type"
    End With
    UserForm1.Show
    Range("InptShingleType").Value = Selection
    UserForm1.Hide
    End Sub

    I've tried declaring a variable with the same name in the user form, but
    that didn't work either. Thanks.

  2. #2
    Tom Ogilvy
    Guest

    Re: Pass variable from user form to macro

    Sub PopulateListWithShingleTypeRange()
    'Shingle Type Selection
    Dim Selection As String
    Dim ShingleSeletion As String
    For Each x In Sheet1.Range("StblShingleType")
    UserForm1.ListBox1.AddItem x.Value
    Next
    UserForm1.Caption = "Shingle Selection"
    With UserForm1.Label1
    ' Set the text of the label.
    .Caption = "Select the Shingle Type"
    End With
    UserForm1.Show
    Range("InptShingleType").Value = Userform1.ListBox1.Value
    Unload Userform1
    End Sub


    In the userform you should have code to hide the userform.

    An alternate method would be to use the click event of the listbox in the
    userform

    Private Sub Listbox1_Click()
    if listbox1.ListIndex <> -1 then
    Range("InptShingletype").Value = Listbox1.Value
    end if
    End Sub

    --
    Regards,
    Tom Ogilvy



    "r wilcox" <[email protected]> wrote in message
    news:[email protected]...
    > I'm using a user form to get a selection from a customer, which I then

    will
    > place in a cell in the worksheet. How do I pass this selection from the

    user
    > form to the controlling sub?
    >
    > Sub PopulateListWithShingleTypeRange()
    > 'Shingle Type Selection
    > Dim Selection As String
    > Dim ShingleSeletion As String
    > For Each x In Sheet1.Range("StblShingleType")
    > UserForm1.ListBox1.AddItem x.Value
    > Next
    > UserForm1.Caption = "Shingle Selection"
    > With UserForm1.Label1
    > ' Set the text of the label.
    > .Caption = "Select the Shingle Type"
    > End With
    > UserForm1.Show
    > Range("InptShingleType").Value = Selection
    > UserForm1.Hide
    > End Sub
    >
    > I've tried declaring a variable with the same name in the user form, but
    > that didn't work either. Thanks.




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1