ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Passing the value of listbox to other Subs (https://www.excelbanter.com/excel-programming/340004-passing-value-listbox-other-subs.html)

Cygnus241

Passing the value of listbox to other Subs
 

I'm having a major headache trying to define a variable through
listbox and then having that variable passed through to othe
modules/subs.

Basically, I want a listbox which obtains an input from the user an
stores it in a variable. Then I want a second userform which obtains
second variable. Then I want to be able to call a specific sub based o
those two parameters.

My problem is that when I reference the variable from another sub th
value shows as "". I've tried to define a Public variable but tha
only seems to work within one module at a time.

Below is the code that I'm trying to use. Any help is greatl
appreciated. Thx!!


Sub WorkBook_Open()

Call ChooseLocation
ActiveWorkbook.Save
ActiveWorkbook.Close
End Sub

Sub ChooseLocation()
LocationForm.ListBox1.AddItem "Location1"
LocationForm.ListBox1.AddItem "Location2"
LocationForm.ListBox1.AddItem "Location3"

LocationForm.Show

End Sub

Sub CommandButton1_Click()
If Location = "Location3" Then
Call Location3Routine
' Above routine needs to be created later
Else
Call ChooseOption
End If

Unload LocationForm

End Sub

Sub ListBox1_Click()
Location = ListBox1.Value ** This is the variable that I need t
reference in later areas of the program **
End Sub

Sub ChooseOption()
OptionForm.Show
End Sub


Sub OptionButton1_Click()
End Sub

Sub OptionButton2_Click()
End Sub

Sub OptionButton3_Click()
End Sub

Sub OptionButton4_Click()
End Sub

Sub SelectionButton_Click()
If Location = "Location1" Then
** Here's where I want to define additional variables based on th
value of LOCATION except I cannot find a way to carry that valu
through to this point of the program **

' Define variables based on Location
End If

If OptionButton1 Then
' Code for Option1
End If

Unload OptionForm

End Su

--
Cygnus24
-----------------------------------------------------------------------
Cygnus241's Profile: http://www.excelforum.com/member.php...fo&userid=2722
View this thread: http://www.excelforum.com/showthread.php?threadid=46738


David Adamson[_4_]

Passing the value of listbox to other Subs
 
Two options you might want to consider

1. Export/Import
The easist way I dealt with this sort of stuff is to save the choice to a
spreadsheet once its selected.
I generally have a worksheet that contains all the choices and the option a
user selects.
This way I always have a hard copy of the choices a user selects for later
reference.

Then simply import the data back in to use.

2. Make a function to pass variables

Alternatively you could just make the other sub a function and run it that
way

Sub Dummy
Dim Option as String
Dim result as string
Dim i as intiger

Option = listbox1.value
i = 10
result = Values(Option, i)

End Sub

Function Values(Option As String, i As Integer)
....
Values = ...
End Function

Hope this helps





All times are GMT +1. The time now is 05:37 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com