第一次用按键精灵写程序,实现在多个excel搜索内容,输出正行内容,不过缺点很多,按键精灵提供的函数过少,对excel仅仅提供了读取写入单元格的功能,字符串也没有提供子字符串的判断功能。
Event MainForm.ShowDirFileBtn.Click
If MainForm.BrowseFolderBox.Path <> "" Then
Files = Lib.文件.遍历指定目录下所有文件名(MainForm.BrowseFolderBox.Path)
For i = 0 To UBound(Files)
If StrComp(Split(Files(i), ".")(1), "xls") OR StrComp(Split(Files(i), ".")(1), "xlsx") Then
MainForm.FileListBox.AddItem MainForm.BrowseFolderBox.Path & "/"& Files(i)
End If
Next
End If
End Event
//双击删除所选行
Event MainForm.FileListBox.DblClick
MainForm.FileListBox.RemoveItem MainForm.FileListBox.ListIndex
End Event
//搜索内容
Event MainForm.SearchBtn.Click
If MainForm.SearchTextBox.Text = "" Then
MessageBox "搜索内容不能为空"
Exit Event
End If
If MainForm.FileListBox.ListCount = 0 Then
MessageBox "没有选中文件"
End If
FilesArray = Split(MainForm.FileListBox.List,"|")
searchText = MainForm.SearchTextBox.Text
result = ""
//遍历文件
For i = 0 To UBound(FilesArray)
curFile = FilesArray(i)
Call Plugin.Office.OpenXls(curFile)
curFileSearch = Split(curFile,".")(1) & ":"
pageCnt = 1
//统计页数
While Plugin.Office.ReadXls(pageCnt, 1, 1)<> ""
pageCnt = pageCnt + 1
Wend
//搜索页
For page = 1 to pageCnt-1
//查找当前页的行数目
rowCnt = 1
While Plugin.Office.ReadXls(page, rowCnt, 1) <> ""
rowCnt = rowCnt + 1
Wend
//查找列数
colCnt = 1
While Plugin.Office.ReadXls(page, 1, colCnt) <> ""
colCnt = colCnt + 1
Wend
//按行读取
For row = 1 To rowCnt - 1
If MainForm.DetailCheckBox.Value = 1 Then
rowText = curFileSearch & page & "页," & row & "行:"
Else
rowText = ""
End If
FindFlag = false
For col = 1 To colCnt-1
cellText = Plugin.Office.ReadXls(page, row, col)
rowText = rowText & cellText & " "
If StrComp(cellText,searchText)=0 Then
FindFlag = true
End If
Next
If FindFlag = true Then
result = result & rowText & vbCrLf
End If
Next
Next
Call Plugin.Office.CloseXls()
Next
If result = "" Then
MessageBox "没有搜索到匹配选项"
Else
MainForm.ResultBox.Text = result
End If
End Event
Event MainForm.Load
MainForm.FileListBox.List = ""
MainForm.BrowseFolderBox.Path = ""
MainForm.BrowseFileBox.Path = ""
End Event
Event MainForm.AddFileBtn.Click
If MainForm.BrowseFileBox.Path <> "" Then
MainForm.FileListBox.AddItem MainForm.BrowseFileBox.Path
End If
End Event