VB get data from excel - validation

user3498380

I have a code which is mainly working except I have a validation error... My code has a search box where I put the surname in, the idea is that if the surname entered is in the excel sheet and a bunch of information is returned. This works as desired, as does my validation for no data entered in the search box.

What doesn't work is my validation for when I enter something in the search box which isn't a match (i.e. not in the excel sheet). Please see 'match validation' in the following code to see what I'm referring to do.

I just have no idea why it isn't working. I don't even get an error when I run the code and enter in wrong data, it just doesn't return an error message like it should and the form sort of freezes up (kinda like its in a non stop loop).

Any advice would be great, Thanks! Here some of the code:

    '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
APrough

This section of code

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

will continue to loop through the worksheet until it finds a match. Let's assume you get to the last row of your data with no match yet. The next "A" & getNum will be blank, which will not be a match, and so it will continue the While, until it hits your row limit and errors out. It never finds a match, so it will never hit your check.

However, I think if you put a check in there to look for those blank cells...

 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

,then I think it will do what you are looking to do.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Import specific data from excel to datagrid vb.net

From Dev

Export data from VB to Excel sheet

From Dev

Excel Data validation list from single cell

From Dev

VB Excel - When opening a word doc from VB with Excel I get a 'Variable not defined' error when trying to use ActiveDocument

From Dev

vb.net: Get data from Xelement <table></table>

From Dev

get possible values from validation list in excel macro

From Dev

Excel - Data Validation list from filtered table

From Dev

VB.NET using Linq to get data from Listview

From Dev

Get all data from Buttons extension (Excel)

From Dev

Import specific data from excel to datagrid vb.net

From Dev

How to read and get data from excel .xlsx

From Dev

Get data from another Excel file

From Dev

how to get data from database and put it on the textbox - vb.net

From Dev

Excel Data Validation as input to another Data Validation

From Dev

Removing blank entries from Excel data validation with dependant lists

From Dev

Perform data validation from table on different worksheet in Excel 2007

From Dev

Excel - Data Validation list from filtered table

From Dev

MS Excel - Data Validation

From Dev

Data Validation From 2 Lists EXCEL 2010

From Dev

Get Populate data from looping vb.net

From Dev

Copy data from vb.net datagrid into excel using sendkeys

From Dev

Get specific data from Excel column

From Dev

Excel, get data from matrix

From Dev

How to get data from MS Access to listview VB6

From Dev

Exporting data from DataGridView as Excel table using VB.NET

From Dev

How to get the cell row from validation list in Excel

From Dev

Vb.net how to get Time data from access?

From Dev

when completed with form on VB for excel my data doesnt get entered into excel

From Dev

How to get data from database mysql with For clause in Vb net

Related Related

  1. 1

    Import specific data from excel to datagrid vb.net

  2. 2

    Export data from VB to Excel sheet

  3. 3

    Excel Data validation list from single cell

  4. 4

    VB Excel - When opening a word doc from VB with Excel I get a 'Variable not defined' error when trying to use ActiveDocument

  5. 5

    vb.net: Get data from Xelement <table></table>

  6. 6

    get possible values from validation list in excel macro

  7. 7

    Excel - Data Validation list from filtered table

  8. 8

    VB.NET using Linq to get data from Listview

  9. 9

    Get all data from Buttons extension (Excel)

  10. 10

    Import specific data from excel to datagrid vb.net

  11. 11

    How to read and get data from excel .xlsx

  12. 12

    Get data from another Excel file

  13. 13

    how to get data from database and put it on the textbox - vb.net

  14. 14

    Excel Data Validation as input to another Data Validation

  15. 15

    Removing blank entries from Excel data validation with dependant lists

  16. 16

    Perform data validation from table on different worksheet in Excel 2007

  17. 17

    Excel - Data Validation list from filtered table

  18. 18

    MS Excel - Data Validation

  19. 19

    Data Validation From 2 Lists EXCEL 2010

  20. 20

    Get Populate data from looping vb.net

  21. 21

    Copy data from vb.net datagrid into excel using sendkeys

  22. 22

    Get specific data from Excel column

  23. 23

    Excel, get data from matrix

  24. 24

    How to get data from MS Access to listview VB6

  25. 25

    Exporting data from DataGridView as Excel table using VB.NET

  26. 26

    How to get the cell row from validation list in Excel

  27. 27

    Vb.net how to get Time data from access?

  28. 28

    when completed with form on VB for excel my data doesnt get entered into excel

  29. 29

    How to get data from database mysql with For clause in Vb net

HotTag

Archive