|
楼主,我修改了一段代码,运行的时候出现类型不匹配,帮忙看下啥情况呗~
Option Explicit
Private Sub TextBox1_Change() '文本框内容改变时调用filllvw过程查询
Dim c As Byte
Dim i As Integer
If ComboBox1.Text = "" Then Exit Sub
c = WorksheetFunction.Match(ComboBox1.Text, ArrCaption, 0) '获取列号
Call FillLvw(ListView1, c, TextBox1.Text)
End Sub
Private Sub UserForm_Initialize()
Dim ArrWidth '定义listview列宽
Dim c As Integer '表列数
Dim i As Integer
With Sheet1
c = .Range("C1").End(xlToRight).Column
If c = 256 Then Exit Sub
ArrCaption = .Range("C1:" & Chr(64 + c) & "1")
ReDim ArrWidth(3 To c)
For i = 3 To c
ArrWidth(i) = .Cells(1, i).Width '记录单元格宽度
Next
End With
ComboBox1.Style = fmStyleDropDownList
ComboBox1.List = WorksheetFunction.Transpose(ArrCaption) '组合框赋值
With ListView1
.View = lvwReport 'listview视力类型为表格型
.FullRowSelect = True '整行可以选中
.Gridlines = True '显示网格线
.LabelEdit = lvwManual '禁止编辑
For i = 3 To c '循环添加标题并设置宽度
.ColumnHeaders.Add , , ArrCaption(1, i - 2), ArrWidth(i), 0
Next
End With
Call FillLvw(ListView1, 1, "")
End Sub
Sub FillLvw(Lvw As ListView, QueryColumn As Byte, QueryStr As String) '此过程用于填充listview项或查询
Dim Arr()
Dim Item As ListItem 'ListView列表项
Dim r As Integer '表总行数
Dim c As Integer '表总列数
Dim i As Integer
Dim n As Byte
With Sheet1
c = .Range("C1").End(xlToRight).Column '取得列号
r = .Range("C65536").End(xlUp).Row '取得总行号
If r = 1 Then Exit Sub
Arr = .Range("C2:" & Chr(64 + c) & r) '初始化数组
End With
Lvw.ListItems.Clear '清除Listview所有项
For i = 1 To r - 1 '循环赋数组值给Lvw
If InStr(1, Arr(i, QueryColumn), QueryStr) > 0 Then '判断数组某列中是否包含要查询的内容,如果有就添加列表项
Set Item = Lvw.ListItems.Add()
Item.Text = Arr(i, 1) '列表项文本
For n = 2 To c - 2
' Item.SubItems(n - 1) = Arr(i, n) '列表项后面几列文本
Next
Set Item = Nothing
End If
Next
End Sub |
|