停止搜索找到

时间:2015-06-29 22:30:11

标签: vb.net

我有一个搜索.xls文档的应用程序。该文件是60000行。我想要做的是,在找到结果时停止搜索。目前一切正常,但它会扫描剩下的59999行。

 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
        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()

1 个答案:

答案 0 :(得分:1)

从Plutonix修复有效。谢谢!

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()