列表框填充特定行
Listbox populate with specific rows
我只想从数据库中填充特定行的 vba 列表框。
这就是我得到的。
Private Sub UserForm_Initialize()
Hoja2.Activate
ListBox1.ColumnCount = 5
ListBox1.ColumnWidths = "70;90;90;90;70"
ListBox1.AddItem "FIRST NAME"
ListBox1.List(0, 1) = "LAST NAME"
ListBox1.List(0, 2) = "LAST NAME 2"
ListBox1.List(0, 3) = "BORN DATE"
ListBox1.List(0, 4) = "AGE"
Dim seguimiento As Integer
Dim i As Integer
seguimiento = Application.WorksheetFunction.CountA(Range("b:b"))
For i = 1 To seguimiento
If Cells(i, 20) = "" Then
ListBox1.AddItem Cells(i, 3)
Else
End If
Next i
End Sub`
实际上很难从你的问题中理解列表框的填充标准
假设您要用单元格填充列表框:
- 列 "T" 单元格不为空的行
- 正在扫描从第 2 行向下到第 "B" 列中最后一个非空行的行
- 通过 "G"
从列 "C" 获取值
试试下面的代码:
Option Explicit
Private Sub UserForm_Initialize()
Dim seguimiento As Long, i As Long
Dim Data() As Variant '<--| use an array to store data to eventually fill ListBox list
Dim cell As Range
With Me.ListBox1
.ColumnCount = 5
.ColumnWidths = "70;90;90;90;70"
End With
i = 1
With Hoja2 '<--| refer to your worksheet
With .Range("T1:T" & .Cells(.Rows.Count, "B").End(xlUp).Row).SpecialCells(xlCellTypeConstants) '<--| refer to non blank cells in its column "T" (i.e. with column index 20) from row 1 to last non blank one in column "B"
seguimiento = .Count '<--| count those non empty cells
ReDim Data(1 To seguimiento + 1, 1 To Me.ListBox1.ColumnCount) '<--| redim data array rows accordingly (while setting columns to 5)
'fill data array first row with "headers"
Data(1, 1) = "FIRST NAME"
Data(1, 2) = "LAST NAME"
Data(1, 3) = "LAST NAME 2"
Data(1, 4) = "BORN DATE"
Data(1, 5) = "AGE"
' loop through referred non blank cells and fill subsequent data array rows from corresponding cells in columns "C" through "G"
For Each cell In .Cells
i = i + 1
With cell
Data(i, 1) = .Offset(, -17) '<--| this refers to a cell in the same row as the current cell and in column "C", being offseted 17 columns back from current cell
Data(i, 2) = .Offset(, -16) '<--| this refers to a cell in the same row as the current cell and in column "D", being offseted 16 columns back from current cell
Data(i, 3) = .Offset(, -15) ' etc...
Data(i, 4) = .Offset(, -14) ' etc...
Data(i, 5) = .Offset(, -13) ' etc...
End With
Next cell
End With
End With
ListBox1.List = Data '<---| finally, fill listbox list with data array
End Sub
我只想从数据库中填充特定行的 vba 列表框。 这就是我得到的。
Private Sub UserForm_Initialize()
Hoja2.Activate
ListBox1.ColumnCount = 5
ListBox1.ColumnWidths = "70;90;90;90;70"
ListBox1.AddItem "FIRST NAME"
ListBox1.List(0, 1) = "LAST NAME"
ListBox1.List(0, 2) = "LAST NAME 2"
ListBox1.List(0, 3) = "BORN DATE"
ListBox1.List(0, 4) = "AGE"
Dim seguimiento As Integer
Dim i As Integer
seguimiento = Application.WorksheetFunction.CountA(Range("b:b"))
For i = 1 To seguimiento
If Cells(i, 20) = "" Then
ListBox1.AddItem Cells(i, 3)
Else
End If
Next i
End Sub`
实际上很难从你的问题中理解列表框的填充标准
假设您要用单元格填充列表框:
- 列 "T" 单元格不为空的行
- 正在扫描从第 2 行向下到第 "B" 列中最后一个非空行的行
- 通过 "G" 从列 "C" 获取值
试试下面的代码:
Option Explicit
Private Sub UserForm_Initialize()
Dim seguimiento As Long, i As Long
Dim Data() As Variant '<--| use an array to store data to eventually fill ListBox list
Dim cell As Range
With Me.ListBox1
.ColumnCount = 5
.ColumnWidths = "70;90;90;90;70"
End With
i = 1
With Hoja2 '<--| refer to your worksheet
With .Range("T1:T" & .Cells(.Rows.Count, "B").End(xlUp).Row).SpecialCells(xlCellTypeConstants) '<--| refer to non blank cells in its column "T" (i.e. with column index 20) from row 1 to last non blank one in column "B"
seguimiento = .Count '<--| count those non empty cells
ReDim Data(1 To seguimiento + 1, 1 To Me.ListBox1.ColumnCount) '<--| redim data array rows accordingly (while setting columns to 5)
'fill data array first row with "headers"
Data(1, 1) = "FIRST NAME"
Data(1, 2) = "LAST NAME"
Data(1, 3) = "LAST NAME 2"
Data(1, 4) = "BORN DATE"
Data(1, 5) = "AGE"
' loop through referred non blank cells and fill subsequent data array rows from corresponding cells in columns "C" through "G"
For Each cell In .Cells
i = i + 1
With cell
Data(i, 1) = .Offset(, -17) '<--| this refers to a cell in the same row as the current cell and in column "C", being offseted 17 columns back from current cell
Data(i, 2) = .Offset(, -16) '<--| this refers to a cell in the same row as the current cell and in column "D", being offseted 16 columns back from current cell
Data(i, 3) = .Offset(, -15) ' etc...
Data(i, 4) = .Offset(, -14) ' etc...
Data(i, 5) = .Offset(, -13) ' etc...
End With
Next cell
End With
End With
ListBox1.List = Data '<---| finally, fill listbox list with data array
End Sub