实现窗体功能如下:
1、点击添加表头:BtnAddTitle_Click
2、点击添加记录:BtnAddRecord_Click
代码如下:
Option Explicit
Private Sub BtnAddTitle_Click()
With ListView1
.ColumnHeaders.Clear '添加之前先清空
Dim i As Integer '循环变量
Dim col As Integer '用于记录列数
col = Range("a1").End(xlToRight).Column
For i = 1 To col
If i = 1 Then
.ColumnHeaders.Add i, , Cells(1, i), .Width / col, lvwColumnLeft
Else
.ColumnHeaders.Add i, , Cells(1, i), .Width / col, lvwColumnCenter
End If
Next
.Gridlines = True '显示边框线
.FullRowSelect = True '支持整行选择
.View = lvwReport '设置数据以表格形式显示
End With
End Sub
Private Sub BtnAddRecord_Click()
Dim itm As ListItem
Dim i As Integer, j As Integer
Dim endCol As Integer, endRow As Integer
endCol = Range("a1").End(xlToRight).Column
endRow = Range("a1").End(xlDown).Row
With ListView1
.ListItems.Clear
For i = 2 To endRow
Set itm = .ListItems.Add()
For j = 1 To endCol - 1
itm.Text = Cells(i, 1)
itm.SubItems(j) = Cells(i, j + 1)
Next
Next
End With
End Sub