第一次用按鍵精靈寫程序黄选,實現在多個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
//統(tǒng)計頁數
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