【发布时间】:2015-06-30 21:18:02
【问题描述】:
我有一个可以在 Excel 文档中搜索 ID 号(一组 6 个数字)的功能齐全的应用程序。一旦找到数字,它就会将它们放入标签中。我的问题是,excel 表几乎有 60000 行,而且需要一段时间(当我按名称搜索时甚至更长)。有没有办法加快搜索速度,或者有其他搜索选项?
Private Sub SearchID()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet1 As Excel.Worksheet
Dim rng As Excel.Range
Dim codeabc As String
Dim found As Boolean
Dim i As Integer
If AssociateID.Text = String.Empty Then
popup.Close()
MsgBox("Please make sure 'Associate ID' is filled out")
Exit Sub
End If
xlApp = CreateObject("Excel.Application")
xlBook = xlApp.Workbooks.Open("G:\grps\every\People Report\HRIS Remedy Report.xls")
xlSheet1 = xlBook.Worksheets(1)
rng = xlSheet1.Range("a1:a60000")
codeabc = (AssociateID.Text)
found = False
For i = 1 To rng.Count
If rng.Cells(i).Value = codeabc Then
IDLabel.Text = AssociateID.Text
NameLabel.Text = (rng.Cells(i).offset(0, 1).value())
DepartmentLabel.Text = (rng.Cells(i).offset(0, 3).value())
PositionLabel.Text = (rng.Cells(i).offset(0, 2).value())
found = True
xlBook.Close()
popup.Close()
Exit Sub
End If
Next i
If Not found Then
MsgBox("Associate ID: " & AssociateID.Text & " is not found. Please check the ID and try again")
AssociateID.Clear()
End If
popup.Close()
xlBook.Close()
End Sub
【问题讨论】:
-
看看使用 excel 的内置搜索功能 - 我怀疑你可以编写任何比 Microsoft 更高效的程序。
标签: vb.net