Hello,

i got a table in Excel (or exported to CSV).
There are 3 columns (1. column = specification / 2. columns = value1 / 3. column = value2).
On my vb.net form i got a textbox, 2 labels and a button. If i write in in the textbox "5" it should search in the Excel or CSV-File in columns A (specification" the "5" and then it should show the both values in this row in label1 and label2.

I tried much things but i can only read and show the complete row in only one label and only from csv-file. but with the csv-file my result is like "47,55;33,2;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"

Here is my code

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For Each row As String In IO.File.ReadAllLines("Table.csv")
            If zeile.Contains(TextBox1.Text) Then 
                Label1.Text = row
                'Label2.Text = ????
            End If
        Next
End Sub

In the Attachement you can a screenshot from the Excel/CSV-File.

Can you help me please?

thank you
josh

Excel_CSV.png

Recommended Answers

All 5 Replies

Just split the fields inside each row:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            For Each row As String In File.ReadAllLines("Table.csv")
                ' split the fields from the row '
                Dim vFields() As String = Split(row, ";")
                If vFields(0).Contains(TextBox1.Text) Then ' vFields(0) = specificaion '
                    lblValue1.Text = vFields(1) ' vFields(1) is value 1 '
                    lblValue2.Text = vFields(2) ' vFields(2) is value 2
                End If
            Next
        Catch ex As Exception

        End Try
    End Sub

TableCSV.PNG

Thank you very much.
It´s so simple and I didn´t found it how to split it...

I want a MessageBox if the "specification" is not in the csv-file.
I tried it with this code :

Try
            For Each row As String In File.ReadAllLines("Table.csv")
                ' split the fields from the row '
                Dim vFields() As String = Split(row, ";")
                If vFields(0).Contains(TextBox1.Text) Then ' vFields(0) = specificaion '
                    lblValue1.Text = vFields(1) ' vFields(1) is value 1 '
                    lblValue2.Text = vFields(2) ' vFields(2) is value 2
                Else
                    MsgBox("specification was not found")
                    Exit For
                End If
            Next
        Catch ex As Exception
            MsgBox(ex.Message) 'File was not found or something else
        End Try

But now is the problem its allways the MessageBox "specification was not found". It seems it doesn't search complete... but why?

ok, my fault... I got it.
Thank you!

Click option to further define your search indeed. To search your data is a work sheet or in a entire or work borad within box, select sheet or work board,to search for data in raws or colums in the search box. Microsoft excel saves the formating options you define. If you search the work sheet for data again and cannot find characters you know to be there you may need to clear the formating options from the previous search.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.