简体   繁体   中英

Get the values from a Userform to a Module inside an Excel Macro

I have moderate knowledge in SQL and able to manage simple queries...

Basic intention was to add a drop-down on a Excel macro I created (instead of a text box where user give whatever values he wants) but after so much research I came to know that only a Userform can help me to satisfy my requirement, Now I want to know how a value selected under a Userform, can be made available under a Module, so that I can use those values selected from a drop down for any other purpose. I have created a sample user-form with below codes

For USER-FORM

Private Sub cmdAdd_Click()
  Call Try1
  Unload Me
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

  If CloseMode = vbFormControlMenu Then
    Cancel = True
    MsgBox "Please use the Close Form button!"
  End If
End Sub

Private Sub UserForm_Initialize()
    Dim cPart As Range
    Dim cLoc1 As Range
    Dim ws As Worksheet
    Set ws = Worksheets("LookupLists")

    For Each cPart In ws.Range("PartIDList")
        With Me.cboPart
            .AddItem cPart.Value
            .List(.ListCount - 1, 1) = cPart.Offset(0, 1).Value
        End With
    Next cPart

    For Each cLoc1 In ws.Range("LocationList")
        With Me.cboLocation
            .AddItem "Cluster1"
            .AddItem "Cluster2"
            .AddItem "Location 3"
        End With
    Next cLoc1

    Me.txtDate.Value = Format(Date, "Medium Date")
    Me.txtQty.Value = 1
    Me.cboPart.SetFocus

End Sub

For Module

Sub Rectangle1_Click()
    frmPartLoc.Show
End Sub

Sub Try1()
    Dim Location
    Location = cboLocation.Value
    Part = cboPart.Text
    MsgBox Location.Value
    MsgBox Part.Text
End Sub

actually this code have some issue here

"Location = cboLocation.Value   Part = cboPart.Text"

I guess its possible to get the values on a MODULE that we selected using a USER-FORM. But I am having some mistake with my code, or understand this stuff little differently. Please help me to correct my code.

I take help from a website to create this User form (not using the complete code) if you want to please use this link to get the zip file of the same or you can browse there using below link

http://www.contextures.com/xlUserForm01.html

Thanks in advance for helping me.

Thanks to Tigeravatar who help to find the solution.

We have to mention the USER-FORM name as well prior to combobox name from which the vale we are populating from USER-FORM.

Please find the Corrected code below

Sub Try1()
    Dim Location
    Location = frmPartLoc.cboLocation.Value
    Part = frmPartLoc.cboPart.Value
    MsgBox Location
    MsgBox Part
End Sub

Thanks again for all the comments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM