您好我的Userform中的列表框数据有问题 当我尝试更改我的列表框连接的源文件时,它似乎没有改变
它首先显示了良好的数据,但是当我尝试点击RUN DATE按钮时
它没有使用我的范围中的值被设置为我的排序键
这是我的RUN DATE BUTTON代码,用于排序升序和降序
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
这就是我在列表框中初始化值的方法
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
有没有办法刷新我的列表框? Listbox1.refresh是这样的吗?
注意:我不需要关闭我的Userform并再次打开以查看更新的列表框 因此,当Userform处于活动模式(打开)时,我可以直接更新列表框值..
谢谢
答案 0 :(得分:1)
您可以使用工作表中的List
数据并指定属性,而不是使用var并将数据分配到var
Named Range
。
ListBox1.RowSource = "Name of the Range"
每次要刷新listbox
时,只需在代码中使用上述作业即可。如果您发现任何困难,请告诉我。
答案 1 :(得分:0)
您可以添加刷新过程,然后在按钮的OnClick事件过程中调用它。 注意,我没有测试过这段代码,但它应该按原来问题的要求进行测试。
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