|
楼主 |
发表于 2017-10-21 18:53
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
run time error 424 object required
接上:
On Error Resume Next
Dim j As Integer, K As Integer, z As Integer
With Application.Sheets("日期")
.Activate
.Visible = True
For j = 1 To ListView1.ListItems.Count
K = .Range("A65536").End(xlUp).Row
.Cells(K + 1, 1) = ListView1.ListItems(j)
For z = 2 To 9
.Cells(K + 1, z) = ListView1.ListItems(j).SubItems(z - 1)
Next z
Next j
With .UsedRange
.Columns.AutoFit
.HorizontalAlignment = xlCenter
.Borders.LineStyle = 1
End With
Do
fName = Application.GetSaveAsFilename(fileFilter:="Excel Files (*.xls), *.xls")
Loop Until fName <> False
Sheets("日期").SaveAs Filename:=fName
ActiveWorkbook.Close
End With
End Sub
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
On Error GoTo myerr ' 错误处理
ListView1.SortKey = ColumnHeader.Index - 1 ' 设定排序值的索引,sortkey是从0开始的,所以要减1
If ListView1.SortOrder = lvwDescending Then ' 当前是否为降序
ListView1.SortOrder = lvwAscending ' 按升序
Else
ListView1.SortOrder = lvwDescending ' 按降序
End If
ListView1.Sorted = True ' 激活listview排序
myerr: ' 错误处理
Exit Sub
End Sub
Private Sub TextBox1_Change()
Dim what As String: what = TextBox1.Value
Dim total$, RW&
RW = Sheet1.Range("A65536").End(xlUp).Row
total = 0
If TextBox1.Value = "" Then
' 如果文本框内容为空,导入所有数据
刷新
Exit Sub
Else
Dim rng As Range
Set rng = Sheet1.Range("A2:C" & RW) ' 关键字所在的区域
Dim dic As Object
Set dic = CreateObject("scripting.dictionary")
ListView1.ListItems.Clear ' 清除所有内容
For I = 3 To RW
For ii = 1 To 9
Dim tmp As Range
Set tmp = Sheet1.Cells(I, ii).Find(what, lookat:=xlPart, MatchCase:=False) ' 部分匹配, 不区分大小写
If Not tmp Is Nothing Then
If Not dic.exists(tmp.Row) Then ' 因为是一行多列查找,所以只要有一个单元格符合要求,要需要换行,否则listitem数据重复
dic.Add tmp.Row, ""
Set ITM = ListView1.ListItems.Add()
ITM.Text = Sheet1.Cells(I, 1)
ITM.SubItems(1) = Sheet1.Cells(I, 2)
ITM.SubItems(2) = Sheet1.Cells(I, 3)
ITM.SubItems(3) = Sheet1.Cells(I, 4)
ITM.SubItems(4) = Sheet1.Cells(I, 5)
ITM.SubItems(5) = Sheet1.Cells(I, 6)
ITM.SubItems(6) = Sheet1.Cells(I, 7)
ITM.SubItems(7) = Sheet1.Cells(I, 8)
ITM.SubItems(8) = Sheet1.Cells(I, 9)
total = total + Sheet1.Cells(I, 9).Value
End If
End If
Next ii
Next I
Label2.Caption = "共找到 " & ListView1.ListItems.Count & " 条记录"
Set dic = Nothing
End If
End Sub
Private Sub UserForm_Initialize()
Dim hwnd As Long
Dim lStyle As Long
hwnd = FindWindow("ThunderDFrame", Me.Caption) '找到窗口的句柄
lStyle = GetWindowLong(hwnd, GWL_STYLE) '获得窗口的样式
lStyle = lStyle Or WS_MINIMIZEBOX '在原窗口样式增加最小化按钮
lStyle = lStyle Or WS_MAXIMIZEBOX '进一步增加最大化按钮
lStyle = lStyle Or WS_THICKFRAME '进一步增加窗口边框,使得窗口可以通过鼠标拖拉改变大小
SetWindowLong hwnd, GWL_STYLE, lStyle '将新的窗口样式指定给窗口
ListView1.ColumnHeaders.Add , , "日期", Width / 8
ListView1.ColumnHeaders.Add , , "时间", Width / 7, lvwColumnCenter
ListView1.ColumnHeaders.Add , , "场次", Width / 11, lvwColumnCenter
ListView1.ColumnHeaders.Add , , "考试班级", Width / 7, lvwColumnCenter
ListView1.ColumnHeaders.Add , , "监考1", Width / 11, lvwColumnCenter
ListView1.ColumnHeaders.Add , , "地点1", Width / 11, lvwColumnCenter
ListView1.ColumnHeaders.Add , , "监考2", Width / 11, lvwColumnCenter
ListView1.ColumnHeaders.Add , , "地点2", Width / 11, lvwColumnCenter
ListView1.ColumnHeaders.Add , , "科目", Width / 11, lvwColumnCenter
ListView1.View = lvwReport ' listivew的显示格式为报表格式
ListView1.Sorted = True ' listivew的排序属性为true
ListView1.SortKey = 0 ' listivew排序的索引为 '日期', 从 0 开始
ListView1.Gridlines = True ' 显示网格线
ListView1.FullRowSelect = True ' 允许整行选中
Label2.Caption = ""
Label3.Caption = ""
'设置循环,填充记录
Dim RW As String
RW = Sheet1.Range("A65536").End(xlUp).Row
Dim total As String
total = 0
With Sheet1
For I = 3 To RW
Set ITM = ListView1.ListItems.Add()
ITM.Text = .Cells(I, 1)
ITM.SubItems(1) = .Cells(I, 2)
ITM.SubItems(2) = .Cells(I, 3)
ITM.SubItems(3) = .Cells(I, 4)
ITM.SubItems(4) = .Cells(I, 5)
ITM.SubItems(5) = .Cells(I, 6)
ITM.SubItems(6) = .Cells(I, 7)
ITM.SubItems(7) = .Cells(I, 8)
ITM.SubItems(8) = .Cells(I, 9)
total = total + .Cells(I, 9).Value
Next I
End With
Label2.Caption = "共找到 " & ListView1.ListItems.Count & " 条记录"
TextBox1.SetFocus
End Sub
Private Sub ListView1_DblClick()
With ActiveSheet
.Cells(Selection.Row, Selection.Column).Offset(0, 0) = ListView1.SelectedItem.Text
.Cells(Selection.Row, Selection.Column).Offset(0, 1) = ListView1.SelectedItem.SubItems(1)
.Cells(Selection.Row, Selection.Column).Offset(0, 2) = ListView1.SelectedItem.SubItems(2)
.Cells(Selection.Row, Selection.Column).Offset(0, 3) = ListView1.SelectedItem.SubItems(3)
.Cells(Selection.Row, Selection.Column).Offset(0, 4) = ListView1.SelectedItem.SubItems(4)
.Cells(Selection.Row, Selection.Column).Offset(0, 5) = ListView1.SelectedItem.SubItems(5)
.Cells(Selection.Row, Selection.Column).Offset(0, 6) = ListView1.SelectedItem.SubItems(6)
.Cells(Selection.Row, Selection.Column).Offset(0, 7) = ListView1.SelectedItem.SubItems(7)
.Cells(Selection.Row, Selection.Column).Offset(0, 8) = ListView1.SelectedItem.SubItems(8)
End With
Unload Me
End Sub
Private Sub UserForm_Resize()
On Error Resume Next
With Label2
.Top = Me.Height - 30 - .Height
End With
Label3.Top = Label2.Top
With ListView1
.Width = Me.Width - 12
.Height = Label2.Top - 6 - .Top
.ColumnHeaders(1).Width = .Width / 8
.ColumnHeaders(2).Width = .Width / 7
.ColumnHeaders(3).Width = .Width / 11
.ColumnHeaders(4).Width = .Width / 7
.ColumnHeaders(5).Width = .Width / 11
.ColumnHeaders(6).Width = .Width / 11
.ColumnHeaders(7).Width = .Width / 11
.ColumnHeaders(8).Width = .Width / 11
.ColumnHeaders(9).Width = .Width / 11
End With
End Sub
Sub 刷新()
Dim I&
ListView1.ListItems.Clear
With Sheet1
For I = 3 To .Range("A65536").End(xlUp).Row
Set ITM = ListView1.ListItems.Add()
ITM.Text = .Cells(I, 1)
ITM.SubItems(1) = .Cells(I, 2)
ITM.SubItems(2) = .Cells(I, 3)
ITM.SubItems(3) = Sheet1.Cells(I, 4)
ITM.SubItems(4) = Sheet1.Cells(I, 5)
ITM.SubItems(5) = Sheet1.Cells(I, 6)
ITM.SubItems(6) = Sheet1.Cells(I, 7)
ITM.SubItems(7) = Sheet1.Cells(I, 8)
ITM.SubItems(8) = Sheet1.Cells(I, 9)
total = total + Sheet1.Cells(I, 9).Value
Next I
End With
Label2.Caption = "共找到 " & ListView1.ListItems.Count & " 条记录"
TextBox1.SetFocus
End Sub
|
|