请问如何把工作表中符合条件的记录填写到VBA listbox中?
7条回答 默认 最新
阳光宅男xxb 2023-01-03 14:37关注Private Sub CommandButton1_Click() ' Purpose: fill listbox with range values after clicking on CommandButton1 ' (code could be applied to UserForm_Initialize(), too) ' Note: based on @Siddharth-Rout 's proposal at https://stackoverflow.com/questions/10763310/how-to-populate-data-from-a-range-multiple-rows-and-columns-to-listbox-with-vb ' but creating a variant data field array directly from range in a one liner ' (instead of filling a redimensioned array with range values in a loop) Dim ws As Worksheet Dim rng As Range Dim MyArray ' variant, receives one based 2-dim data field array '~~> Change your sheetname here Set ws = Sheets("Sheet1") '~~> Set you relevant range here Set rng = ws.Range("A1:C" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row) With Me.ListBox1 .Clear .ColumnHeads = False .ColumnCount = rng.Columns.Count '~~> create a one based 2-dim datafield array MyArray = rng '~~> fill listbox with array values .List = MyArray '~~> Set the widths of the column here. Ex: For 5 Columns '~~> Change as Applicable .ColumnWidths = "50;50;50" .TopIndex = 0 End With End Sub ```bash```
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报