2015-06-29 46 views
0

我有一個搜索.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() 
+4

當發現它時加入'Exit For' – Plutonix

+0

@Plutonix那很容易...對我很愚蠢!把它作爲信用的答案! :) – dwb

回答

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