Store UserForm List Selection as Variable

alyssa75

Board Regular
Joined
May 14, 2007
Messages
240
What am I doing wrong?

Private Sub cmdOk_Click()

iMyVariable = ListBox1.Value

Unload Me
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
What's the problem?

Are you trying to use iMyVariable after you've unloaded the form?

Where did you declare it?
 
Upvote 0
Nothing is stored. When I step through the form code, after I made a selection, click ok and it goes past iMyVariable=ListBox1.Value, I hover over iMyVariable and it says NULL.

I have the variable declared as a String in the cmdOK_Click() code.


And yes, my intention is to use iMyVariable after Unload Me.


Any help is appreciated!! Thanks!!
 
Upvote 0
Alyssa

The code you posted is the cmdOK_Click code but there's no variable declarations.

When you step through that code what's the value of Listbox1 before you try and assign it to the variable?

As for using it outside after the form is unloaded, where and how are you going to use it?

You could declare the variable as Public in a standard module and it should be available elsewhere.
 
Upvote 0
Ok...so the first bit of code that calls the form is a Change Event.

NOTE: I changed iMyVariable to iLocation.

Private Sub Worksheet_Change(ByVal Target As range)

Dim iLocation As range

If Target.Column <> 21 Then Exit Sub
If Target.Value > 0 Then
If ActiveCell.Offset(0, -10).Value = "" Then
Load frmLocation
frmLocation.Show
ActiveCell.Offset(0, -10).Value = iLocation
End If
End If
End Sub

_________________________________________________________________________

Here's the entire Userform code:

Private Sub cmdCancel_Click()
Unload Me
End Sub

Private Sub cmdOk_Click()
Dim iLocation As String
iLocation = ListBox1.Value
Unload Me
End Sub

Private Sub UserForm_Initialize()
ListBox1.List = range("Locations").Value
End Sub
 
Upvote 0
You've declared iLocation twice, once in the change event as a Range then in the OK_Click event as a string.

If all you want to do is put the value selected item from the listbox in a cell relative to the one that triggered the change event you don't need any variables.

Code:
Private Sub cmdOk_Click()
 
     ActiveCell.Offset(0, -10).Value = ListBox1.Value
 
     Unload Me 

End Sub
 
Upvote 0
Makes sense. But cell is still not populating. What do I have wrong now? It shows me the form, allows me to select a value but then does not populate anything. And actually, when I test one where the value is not equal to "", it still initializes the form. Ugh.


Private Sub Worksheet_Change(ByVal Target As range)

If Target.Column <> 21 Then Exit Sub
If Target.Value > 0 Then
If ActiveCell.Offset(0, -10).Value = "" Then
Load frmLocation
frmLocation.Show
End If
End If
End Sub


FORM:

Private Sub cmdCancel_Click()
Unload Me
End Sub

Private Sub cmdOk_Click()
ActiveCell.Offset(0, -10).Value = ListBox1.Value
Unload Me
End Sub

Private Sub UserForm_Initialize()
ListBox1.List = range("Locations").Value
End Sub
 
Upvote 0
That sounds like a problem with the change event.

Have you tried stepping through that code?

One thing I noticed about that code was that you only exclude one column.

So if a cell changes anywhere else on the worksheet the code will be triggered.
 
Upvote 0
No other column seems to trigger it - so I think I'm fine there. How do you step through a change event?
 
Upvote 0
I updated the change event code as follows and it now appears to function properly. The only, all be it major, flaw is that the value I select does not populate anywhere.

Private Sub Worksheet_Change(ByVal Target As range)

If Target.Column <> 21 Then Exit Sub
range(Target.Address).Activate
If ActiveCell.Offset(0, -10).Value = "" Then
Load frmLocation
frmLocation.Show
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,542
Messages
6,179,424
Members
452,914
Latest member
echoix

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top