vba-listview控件操作
Private Sub UserForm_Initialize()
Dim i
Dim itm As ListItem
Dim li
Me.ListView1.ColumnHeaders.Add 1, "Q", "QQ号", Me.ListView1.Width / 3 '添加第⼀列
Me.ListView1.ColumnHeaders.Add 2, "N", "昵称", Me.ListView1.Width / 3, lvwColumnCenter '添加第⼆列
Me.ListView1.ColumnHeaders.Add 2, "D", "地区", Me.ListView1.Width / 3, lvwColumnRight '添加第⼆列
Me.ListView1.View = lvwReport '显⽰外观
Me.ListView1.Gridlines = True '是否有表格线
'For i = 2 To [A65535].End(xlUp).Row '使⽤集合的⽅法添加
' Set itm = Me.ListView1.ListItems.Add(Text:=Cells(i, 1))
' Set li = itm.ListSubItems.Add(Text:=Cells(i, 2)) '集合添加,就是添加新的⼀列
' Set li = itm.ListSubItems.Add(Text:=Cells(i, 3)) '添加在上⼀句的后⾯
'
'Next
For i = 2 To [A65535].End(xlUp).Row '使⽤数组的⽅法添加
Set itm = Me.ListView1.ListItems.Add() '先添加⼀个⾏对象,是除开标题(colunmheaders)⾏的⾏对象
itm.Text = Cells(i, 1) '⾏对象的第⼀列
itm.SubItems(1) = Cells(i, 2) '⾏对像的第⼆列
itm.SubItems(2) = Cells(i, 3) '⾏对像的第三列
Next
'subitems是⼀个string 数组
'ListSubItems 是⼀个集合
'me.ListView1.ColumnHeaders.Remove "D" 删除key 是D的列,即此处的地区列
End Sub
'Me.ListView1.View = lvwIcon '普通视图
'Me.ListView1.View = lvwList '列表视图,上下排列,⽆表格线
'Me.ListView1.View = lvwSmallIcon '⼩图标视图
'Me.ListView1.View = lvwReport '报表视图,有标题,可⼼显⽰表格线
Me.ListView1.ListItems.Clear '清除所有列
'筛选功能其实就是写个判断,等于筛选框的值,再添加值即可
'从listview中输⼊数据到表格中时,按列⽅向循环
For i = 1 To Me.ListView1.ColumnHeaders.Count '取列标题个数开始循环,先取第⼀列的标题,,再是第⼆列,第三列 Cells(1, i) = Me.ListView1.ColumnHeaders(i)
For j = 1 To Me.ListView1.ListItems.Count '⾏的循环
If i = 1 Then
Cells(j + 1, i) = Me.ListView1.ListItems(j).Text
Else
Cells(j + 1, i) = Me.ListView1.ListItems(j).SubItems(i - 1)
End If
Next
Next
'选取的⾏输⼊到⼯作表中
Me.ListView1.SelectedItem '表⽰正在选择的⾏
Me.ListView1.SelectedItem.Text '第⼀列的值
Me.ListView1.SelectedItem.SubItems (1) '第⼆列的值
Me.ListView1.SelectedItem.SubItems (2) '第三列的值
'添加红⾊字体⾏
直接⽤增加新的⾏
Set xxx = Me.ListView1.ListItems.Add()
xxx.SubItems(2) = 123
xxx.SubItems(3) = 234
xxx.ForeColor = RGB(255, 0, 0) '列表颜⾊。是第⼀列的颜⾊
xxx.Bold = True '粗体,也是第⼀列的
对于后⾯的要加⾊的话,⽤循环,且⽤集合对象才能设置颜⾊
For x = 2 To Me.ListView1.ColumnHeaders.Count - 1
xxx.ListSubItems(x).ForeColor = RGB(255, 0, 0)
xxx.ListSubItems(x).Bold = True
'批量删除选择的⾏,只针对listview,不删除数据源
Me.ListView1.ListItems.Remove Me.ListView1.SelectedItem.Index '删除单⼀⾏很简单For i = Me.ListView1.ListItems.Count To 1 Step -1
If Me.ListView1.ListItems(i).Selected Then '不带复选框的选择
Me.ListView1.ListItems.Remove ivba排序函数sort用法
' If Me.ListView1.ListItems(i).Checked Then '带复选框的选择
End If
Next
'给⾏加复选框
Me.ListView1.CheckBoxes = True '就带上复选框
Me.ListView1.Sorted = True '允许排序
Me.ListView1.SortKey = Me.ListView1.ColumnHeader - 1 以第⼏列排序
Me.ListView1.SortOrder = lvwAscending '升序,另⼀个是降序
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论