2014-04-07 75 views
0

我有一個主要工作,除了我有一個驗證錯誤... 我的代碼已經在那裏我把姓搜索框代碼,這個想法是,如果輸入的姓是在Excel表格和一堆信息被返回。這可以按需要工作,我的驗證也不會在搜索框中輸入任何數據。VB從Excel中獲取數據 - 驗證

什麼不工作是我的,當我在搜索框中這是不匹配輸入驗證的東西(即不是在Excel工作表)。請參閱以下代碼中的「匹配驗證」以查看我所指的內容。

我只是不知道爲什麼它不工作。我當我運行的代碼和錯誤數據輸入甚至沒有得到一個錯誤,它只是不返回錯誤消息像它應該和表格排序凍結了(有點像其在非停循環)。

任何意見將是巨大的,謝謝!這裏是一些代碼:

'define objects 
    Dim oExcel As Object 
    Dim oBook As Object 
    Dim oSheet As Object 


    'Open a existing workbook and sheet in excel 
    oExcel = CreateObject("Excel.Application") 
    oBook = oExcel.Workbooks.Open(Filename:="c:\users\timothy\desktop\coding\output.xlsx") 
    oSheet = oBook.Worksheets(1) 

    Dim getSurname As String = "" 
    Dim getFirstname As String = "" 
    Dim getAge As String = "" 
    Dim getGender As String = "" 
    Dim getNum As Integer = 1 
    Dim getValidate As Integer = 0  

While oBook.Worksheets(1).Range("A" & getNum).value <> searchInput.Text.ToUpper 
     getNum = getNum + 1 
    End While 

'Length Validation (THIS WORKS) 
If Len(searchInput.Text) = 0 Then 
    getValidate = getValidate + 1 
End If 

'Match validation (THIS DOES NOT WORK) 
If oBook.Worksheets(1).Range("A" & getNum).value <> searchInput.Text.ToUpper Then 
    getValidate = getValidate + 1 
End If 

If getValidate = 0 Then 
    getSurname = oSheet.Range("A" & getNum).Value.ToString 
    getFirstname = oSheet.Range("B" & getNum).Value.ToString 
    getAge = oSheet.Range("C" & getNum).Value.ToString 
    getGender = oSheet.Range("D" & getNum).Value.ToString 
    outputData.Text = "SURNAME: " & getSurname & vbCrLf & "FIRSTNAME: " & getFirstname & vbCrLf & "AGE: " & getAge & vbCrLf & "GENDER: " & getGender & vbCrLf 
Else 
    MsgBox("ERROR!! Please enter valid Quote Number.") 
End If 
+0

是VBA或vb.net? –

+0

嗨,這是VB .... – user3498380

回答

0

這部分代碼

While oBook.Worksheets(1).Range("A" & getNum).value <> searchInput.Text.ToUpper 
     getNum = getNum + 1 
    End While 

將通過工作不斷循環,直到它找到一個匹配。假設您到達數據的最後一行,但尚未匹配。下一個「A」& getNum將爲空白,這不會匹配,因此它將繼續運行,直到它達到行限制和出錯爲止。它從來沒有找到一個匹配,所以它永遠不會打你的支票。

但是,我認爲,如果你把支票在那裏尋找那些空白單元格...

While oBook.Worksheets(1).Range("A" & getNum).value <> searchInput.Text.ToUpper 
     if oBook.Worksheets(1).Range("A" & getNum).value = "" then 
      exit while 
     else 
      getNum = getNum + 1 
     end if 
    End While 

,那麼我認爲這會做你正在尋找做什麼。