+ Reply to Thread
Results 1 to 5 of 5

Help with userform code, please?

Hybrid View

1eyedjack Help with userform code,... 02-19-2010, 07:43 AM
Palmetto Re: Help with userform code,... 02-19-2010, 07:51 AM
1eyedjack Re: Help with userform code,... 02-19-2010, 08:01 AM
romperstomper Re: Help with userform code,... 02-19-2010, 08:52 AM
1eyedjack Re: Help with userform code,... 02-19-2010, 09:10 AM
  1. #1
    Forum Contributor
    Join Date
    11-14-2007
    Posts
    142

    Help with userform code, please?

    Hi I am not experienced in VBA so please if possible respond in context.
    I have a workbook with a globally defined named range "MyArray" which refers to a constant array = {"Item1","Item2","Item3"}
    My general module contains the following

    Option Explicit
    Public bResponse As Boolean
    Public sComboVal As String
    Sub Get_sComboVal
    sComboVal = "Item1"
    UserForm1.Show
    If bResponse = False Then GoTo End_Subroutine
    Unload UserForm1
    ' Various code inserted here
    End_Subroutine:
    ' Various code inserted here
    End Sub 'Get_sComboVal
    UserForm1 contains just four objects:
    CommandButton1 displays "OK"
    CommandButton2 displays "Cancel"
    ComboBox1
    Label1 describes ComboBox1

    What I am trying to achieve is this:
    If at any time the user clicks "Cancel", the UserForm1 is unloaded and bResponse is returned False. Otherwise:
    User clicks the dropdown arrow on the ComboBox and a list appears containing the values in MyArray.
    User selects a value from that drop down list and confirms by clicking "OK".
    That newly selected value is then assigned to the variable sComboVal
    And then the routine in the general module that called the userform undertakes various actions based on the newly assigned value of sComboVal.

    So far, all I have managed to achieve in the code behind the userform is to define what happens when the user clicks OK or cancel, thus:

    Option Explicit
    Private Sub CommandButton1_Click()
    bResponse = True
    UserForm1.Hide
    End Sub
    Private Sub CommandButton2_Click()
    bResponse = False
    UserForm1.ComboBox1.Value = sComboVal 'not sure that this is correct
    Unload UserForm1
    End Sub
    At this point I get lost, that is to say programming the code for the ComboBox1,
    (a) so that it lists the values of MyArray, and
    (b) to assign to sComboVal the value selected from that list by the user.

    Can someone please help me with these last steps (and correct any obvious errors so far)?

    Thanks
    Last edited by 1eyedjack; 02-19-2010 at 09:11 AM.

  2. #2
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Help with userform code, please?

    Your post does not comply with the Forum Rules you agreed to follow. Per Rule #3, all VBA code must be wrapped in code tags. Please edit your thread to apply the code tags, after which solutions to your question will be suggested.

    Rule #3
    Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # button at the top of the post window. If you are editing an existing post, press Go Advanced to see the # button. For more information about these and other tags, click here.
    Palmetto

    Do you know . . . ?

    You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.

  3. #3
    Forum Contributor
    Join Date
    11-14-2007
    Posts
    142

    Re: Help with userform code, please?

    Done, with apologies and thanks for any help

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,978

    Re: Help with userform code, please?

    Roughly speaking:

    Normal module:
    Option Explicit
    Sub Get_sComboVal()
        Dim frm As UserForm1
        Dim sComboVal As String
        Set frm = New UserForm1
        sComboVal = "Item1"
        frm.Show
        If frm.bResponse Then
            sComboVal = frm.ComboBox1.Text
            Unload frm
            Set frm = Nothing
        ' Various code inserted here
        End If
        ' Various code inserted here
    End Sub 'Get_sComboVal
    Userform module:
    Option Explicit
    Public bResponse As Boolean
    Private Sub CommandButton1_Click()
        bResponse = True
        Me.Hide
    End Sub
    Private Sub CommandButton2_Click()
        bResponse = False
        Me.Hide
    End Sub
    
    Private Sub UserForm_Initialize()
        Me.ComboBox1.List = [MyArray]
    End Sub
    
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
        If CloseMode = vbFormControlMenu Then CommandButton2_Click
    End Sub
    Everyone who confuses correlation and causation ends up dead.

  5. #5
    Forum Contributor
    Join Date
    11-14-2007
    Posts
    142

    Re: Help with userform code, please?

    Beautiful, 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