How to keep user selection from a listbox after userform unloads?

Staliro

New Member
Joined
Mar 8, 2021
Messages
1
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Hello Beautiful people,
Pls i am new to excel vba . i have a problem with a project i am creating.
I have list of items(Tanks) in a listbox. Anytime i select an item(tank) and Add, the the userform unloads and the listbox selection also unloads.
i don't want to keep on choosing a tank after the userform unloads
i want the selected tank to remain even if the userform unloads. Below is the code .

VBA Code:
Private Sub lstTankName_Click()
On Error Resume Next

cmdAdd.Enabled = True
lblDate.Visible = False
lblTransuctionID.Visible = False
txtQuantity.Value = ""
txtKG.Value = ""

For x = 0 To lstTankName.ListCount - 1
lblTankCode.Caption = lstTankName.List(lstTankName.ListIndex, 0)
lblTkName.Caption = lstTankName.List(lstTankName.ListIndex, 1)

If lstTankName.Value = 1 Then
Unload lstTankName
lstTankName.ListIndex = 1
End If
Next x

If Sheet2.Range("B:B") = UserForm1.lblTankCode Then

m = Application.WorksheetFunction.SumIf(Sheet2.Range("B:B"), UserForm1.lblTankCode, Sheet2.Range("D:D"))
n = Application.WorksheetFunction.SumIf(Sheet3.Range("B:B"), UserForm1.lblTankCode, Sheet3.Range("D:D"))
p = n - m
lblStock.Caption = p
End If

End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
you need to declare a global variable.
do this in a Module (not the userform code area)

VBA Code:
Public SelectedTankCode as long
 
Upvote 0
I guess if you want to remove from the screen for a while, then just hide the UserForm.

The other option would be saving the ListIndex somewhere like hidden sheet as memory.
Sheet("Hidden").Range("A1").Value=Me.ListBox1.ListIndex

When reinitialized
Me.ListBox1.ListIndex = Sheets("Sheet2").Range("A1").Value

This will highlight the last selected value the next day ;)
 
Upvote 0

Forum statistics

Threads
1,214,927
Messages
6,122,311
Members
449,080
Latest member
jmsotelo

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