1、ListBox控件在数据展示与交互中优势显著。它能高效容纳多条数据,支持滚动查看,节省界面空间。在查询、登录等应用上都能起到很好的目视效果与交互效果。下例中学习如何在数据录入后让录入数据在ListBox中显示出来,避免录入重复或漏录入的情况发生,让数据一目了然!!!
2、ListBox列表框的常用属性
columncount 列数
columnheads 列头
columnwidths 列宽
名称 名称
backcolor 背景颜色
bordercolor 边框颜色
borderstyle 边框透明
controltiptext 提示文本
forecolor 字体颜色
specialeffect 边框样式
visible 可见
height 高
left 距离左边
top 距离上边
locked 锁
textalign 文本左中右对齐
font 字体
自行尝试调整各个属性,看一下列表框发生什么变化?
3、数据写入ListBox列表框的方法(数组)
Private Sub UserForm_Initialize()
Dim arr As Variant
Dim r%
r = Sheet1.Range("A1048576").End(xlUp).Row
‘查找非空单元格行号
arr = Sheets("listbox").Range("a1:d" & r)
‘数据写入数组
UserForm1.ListBox1.List = arr
‘数组写入列表框
End Sub
*注意:需要提前在属性中设置好列数和列宽,当然可以在代码中写。两者看
4、完善上一节讲的设备点检表(追加ListBox列表框)
(代码如下)
Private Sub UserForm_Initialize()
设备点检表2.设备编号.List = Array("AY-01", "AY-02", "AY-03", "AY-04")
设备点检表2.ListBox1.ColumnCount = 8
设备点检表2.ListBox1.ColumnWidths = "55,35,35,25,25,25,25,25"
'在窗体初始化时设置列表框的列数和列宽
End Sub
Private Sub 退出_Click()
Application.DisplayAlerts = False
'保存时关闭警告提示信息
ThisWorkbook.Save
Application.Quit
End Sub
Private Sub 确定_Click()
Dim r
Dim arr As Variant
r = Cells(Rows.Count, "a").End(xlUp).Row + 1
Cells(r, "a") = Date
Cells(r, "b") = 点检人.text
Cells(r, "c") = 设备编号.text
If ok1.Value = True Then
Cells(r, "d") = "OK"
Else
Cells(r, "d") = "NG"
End If
If ok2.Value = True Then
Cells(r, "e") = "OK"
Else
Cells(r, "e") = "NG"
End If
If ok3.Value = True Then
Cells(r, "f") = "OK"
Else
Cells(r, "f") = "NG"
End If
If ok4.Value = True Then
Cells(r, "g") = "OK"
Else
Cells(r, "g") = "NG"
End If
If ok5.Value = True Then
Cells(r, "h") = "OK"
Else
Cells(r, "h") = "NG"
End If
arr = Range("a2:j" & r)
设备点检表2.ListBox1.List = arr
End Sub
你学会了吗?不论你是求知若渴的在校学生,还是在职场摸爬滚打多年的上班族,只要怀揣着提升自我的决心,别犹豫,快点赞、关注、收藏!我会始终站在零基础小白的视角,循序渐进,带你一步步踏入VBA的奇妙世界。
