Home » excel » excel vba – VBA Refresh UserForm ListBox Data when source changes

excel vba – VBA Refresh UserForm ListBox Data when source changes

Posted by: admin April 23, 2020 Leave a comment

Questions:

Hi I have encountered problem with my listbox data in my Userform
When I try to change the source file where my listbox connected it doesn’t seems to change

It was showing good data at first but when I try to click RUN DATE button

enter image description here

It doesn’t go with the Value in my Range that is being set as My key for sorting

enter image description here

Here is my code for RUN DATE BUTTON for sorting Ascending and Descending

Private Sub CommandButton1_Click()

Application.EnableEvents = False

Worksheets("combobox_value").Activate
Dim strDataRange As Range
Dim keyRange As Range
Set strDataRange = Range("I2:L4")
Set keyRange = Range("I2:I4")

If Range("M2").Value = "D" Then

strDataRange.Sort Key1:=keyRange, Order1:=xlDescending
Range("M2").Value = "A"
Else
strDataRange.Sort Key1:=keyRange, Order1:=xlAscending
Range("M2").Value = "D"
End If
Application.EnableEvents = True
End Sub

And this is how I initialize the value in my listbox

Private Sub UserForm_Initialize()
'set ListBox properties on initialization of UserForm
Set sht = ThisWorkbook.Worksheets("combobox_value")
lastRow_combobox_column = sht.Cells(sht.Rows.Count, "I").End(xlUp).Row


With ListBox1
.ColumnCount = 4
.ColumnWidths = "100"
.ColumnHeads = False
.ControlTipText = True
End With

'Load Worksheet Range directly to a ListBox:
Dim var As Variant


var = Sheets("combobox_value").Range("I2:L" & lastRow_combobox_column)


Me.ListBox1.List = var


End Sub

Is there a way to refresh my listbox? Listbox1.refresh something like that?

Note: I don’t need to close my Userform and open again to see the updated listbox
so while the Userform is in active mode(Open) I can directly update the listbox value..

Thanks

How to&Answers:

Instead of using var and assigning the data to List from var, you can use Named Range of data in the sheet and assign the property
ListBox1.RowSource = "Name of the Range"

Every time you want to refresh the listbox just use the above assignment in your code and it will work. If you find any difficulty please let me know.

Answer:

You could add a refresh procedure, then call it in your OnClick event procedure for the button.
Note, I haven’t tested this code, but it should do what your original question asked.

Private Sub UserForm_Initialize()
    'set ListBox properties on initialization of UserForm
    Set sht = ThisWorkbook.Worksheets("combobox_value")
    lastRow_combobox_column = sht.Cells(sht.Rows.Count, "I").End(xlUp).Row

    With ListBox1
        .ColumnCount = 4
        .ColumnWidths = "100"
        .ColumnHeads = False
        .ControlTipText = True
    End With

    RefreshListbox 
End Sub

Private Sub CommandButton1_Click()    
    Application.EnableEvents = False

    Worksheets("combobox_value").Activate
    Dim strDataRange As Range
    Dim keyRange As Range
    Set strDataRange = Range("I2:L4")
    Set keyRange = Range("I2:I4")

    If Range("M2").Value = "D" Then

        strDataRange.Sort Key1:=keyRange, Order1:=xlDescending
        Range("M2").Value = "A"
    Else
        strDataRange.Sort Key1:=keyRange, Order1:=xlAscending
        Range("M2").Value = "D"
    End If
    Application.EnableEvents = True
    RefreshListbox
End Sub

Private Sub RefreshListbox()
    Me.ListBox1.Clear
    'Load Worksheet Range directly to a ListBox:
    Dim ListRange As Range
    ListRange = Sheets("combobox_value").Range("I2:L" & lastRow_combobox_column)
    Me.ListBox1.List = ListRange
End Sub