holding value in listbox on a userform

rickblunt

Well-known Member
Joined
Feb 18, 2008
Messages
609
Office Version
  1. 2019
Platform
  1. Windows
Greetings - Can anyone confirm for me when the values placed in a listbox on a userform are removed? I always thought it would be whenever the userform was closed (or perhaps reinitialized), and that as long as you kept it open the value in that listbox would not be null. I am using the code below to transfer data from a userform to two different WS's and I am pretty sure it is correct. It works perfectly on the "Device List" section , but as soon as it moves to the "Repair Log" portion it fails to find the correct row for the data to be entered, and places the data in the first blank row.

I tracked it down to the listbox value becoming "null" after it executes the Device List portion of the code. While I do close and reopen the userform much later in the code to refresh all of the data on the userform, I haven't done that at this point in the code so I do not understand why I am losing the value in the DeviceId listbox. (Removing the code to close and reopen the userform did not correct the issue.)

Does anyone have any thoughts on this - or is it this not the way it works? I appreciate the input.


VBA Code:
Private Sub CommentButton_Click()

    Dim i As Long
    Dim Lastrow As Long
    Dim Found As Range
          
    Worksheets("Device List").Activate
    
    Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    
        For i = 1 To Lastrow
            If Cells(i, 8).Value = "FAIL" And Cells(i, 7).Value = "" Then
            Cells(i, 7).Value = RepairComments.Value
                
    'Sheets("Repair Log").Activate        
        
       Set Found = Range("'Repair Log'!A:A").Find(DeviceId.Value, , xlValues, xlWhole)
    If Found Is Nothing Then
        MsgBox "No match for " & DeviceId.Value, , "No Match Found"
    ElseIf Found(1, 3).Text = "" Then
        Found(1, 3).Value = RepairComments.Value
    Else
        MsgBox "No match for " & DeviceId.Value, , "No Match Found"
    End If
    
    Exit For
    End If
    Next
' and a lot more code after this - later on in this code I do reinitialize this userform to update the data in it.....
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Can anyone confirm for me when the values placed in a listbox on a userform are removed?
- when its Clear method is invoked;
- when its RemoveItem method is invoked on all its rows;
- when the Userform has been terminated: either closed by the user using the top right X close button or unloaded by the process which loaded the userform.

as long as you kept it open the value in that listbox would not be null.
A Listbox's Value property cannot be used (and always will be Null) when its MultiSelect property is set to frmMultiSelectMulti (=1) or frmMultiSelectExtended (=2).

Hopefully this helps.
 
Upvote 0
- when its Clear method is invoked;
- when its RemoveItem method is invoked on all its rows;
- when the Userform has been terminated: either closed by the user using the top right X close button or unloaded by the process which loaded the userform.


A Listbox's Value property cannot be used (and always will be Null) when its MultiSelect property is set to frmMultiSelectMulti (=1) or frmMultiSelectExtended (=2).

Hopefully this helps.
Thank you for the knowledge - it very helpful . That was also good information about the value properties - I have mine set to fmMltiSelectSingle,
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,198
Members
449,072
Latest member
DW Draft

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