Avatar of prodempsey
prodempseyFlag for United States of America

asked on 

Excel VBA listbox store value selected in variable use in another sub

I'm hoping someone can help me out with this, I haven't worked with userforms much in the past. Thanks in advance for your help.  I created a userform with a listbox, and I'm using code for the listbox in a private sub  within the userform.  However, I can't seem to pass the value from the selected item in the listbox to another sub routine in Module 3.  I tried declaring the variable as a public variable, but that didn't seem to work either.  I also tried to declare a public variable in the module, but that didn't work either.  What am I doing wrong?  I uploaded the file if that'll help.

Here's the code for the listbox:

Private Sub UserForm_Initialize()
    Dim rheadings As Range
    Dim cl As Range
    Set rheadings = Worksheets(9).Range("A4:AA4")
    For Each cl In rheadings
        Me.ListBox1.AddItem cl.Value
    Next cl
    
End Sub

Open in new window


Here's the code for the command button in the userform:

Public PartsDeptLBValue As String
Private Sub PartsDeptButton_Click()
    Dim Msg As String
    Dim i As Integer
    

'If ListBox1.ListIndex <> -1 Then
'MsgBox "Select something"
'Else
Msg = "You selected" & vbNewLine
For i = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(i) Then
        Msg = Msg & ListBox1.List(i) & vbNewLine
End If

    Next i
    MsgBox Msg

ListBox1.Value = PartsDeptLBValue
        
Unload PartsDeptUserForm

End Sub

Open in new window


Here's the code in Module 3 that I used to test if the variable was stored:

Sub GetPartsDept_UserForm_Value()
PartsDeptUserForm.Show

MsgBox PartsDeptLBValue
End Sub

Open in new window

TestBuySellTemplate.xlsm
Microsoft ExcelVBA

Avatar of undefined
Last Comment
Subodh Tiwari (Neeraj)
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Declare the following variable in a Standard Module like Module1 etc. not on Form Module.

Public PartsDeptLBValue As String

Open in new window

Avatar of prodempsey
prodempsey
Flag of United States of America image

ASKER

I tried declaring the public variable in Module 3 at the top, and not on the userform module, but it didn't work.
I am busy right now, will look at your file after a while if no one else replies to you. Please wait...
Avatar of prodempsey
prodempsey
Flag of United States of America image

ASKER

Thanks sktneer!  Hopefully I can find a solution.  I've been pulling my hair out, and it's probably something really simple that I'm missing.
ASKER CERTIFIED SOLUTION
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of prodempsey
prodempsey
Flag of United States of America image

ASKER

That's it!   Thanks sktneer!
You're welcome. Glad to help.
Microsoft Excel
Microsoft Excel

Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.

144K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo