Hi,

I had a longer break, but now I want to continue working on my little project to make my work easier.

I have an Excel file which I converted to CSV because there is no Excel on the target system.

I would like to search for a number or word via vb.net. This word can appear in all columns! If the word was found, then
the cell in column A, B, D, AA and AB is to be output in the respective labels from this line. (lbl_Cell_A, lbl_Cell_B, lbl_Cell_D, lbl_Cell_AA, lbl_Cell_AB)

Unfortunately I only know how to search in the first column (A) and only output columns one after the other.
Here is my code:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            For Each row As String In File.ReadAllLines("Test.csv")
                ' split the fields from the row '
                Dim vFields() As String = Split(row, ";")
                If vFields(0).Contains(TextBox1.Text) Then
                    lbl_Cell_B.Text = vFields(1)
                    lbl_Cell_C.Text = vFields(2)
                End If
            Next
        Catch ex As Exception
            MessageBox.Show("Error : " & ex.Message)
        End Try
    End Sub

Can you help me, please?

Recommended Answers

All 3 Replies

Try this:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            Dim sSearch As String = LCase(TextBox1.Text)
            If sSearch = "" Then Exit Try
            Dim line As Int32 = 0
            TextBox2.Text = ""
            For Each row As String In File.ReadAllLines("Test.csv")
                ' split the fields from the row '
                line += 1
                Dim vFields() As String = Split(row, ";")
                Dim iLbl As Int32 = 0
                For iLbl = 0 To vFields.Length - 1
                    If InStr(LCase(vFields(iLbl)), sSearch) Then
                        Exit For
                    End If
                Next
                If iLbl < vFields.Length Then
                    ' Found: " '
                    TextBox2.Text += "line#=" + line.ToString + " "
                    ' TextBox2 is Multiline '
                    '                                A  B  D  AA  AB '
                    Dim vColumnsToShow() As Int32 = {0, 1, 3, 26, 27}
                    Dim vLabel() As String = {"A", "B", "D", "AA", "AB"}
                    For iLbl = 0 To vColumnsToShow.Length - 1
                        Dim Col_in_Excel As Int32 = vColumnsToShow(iLbl)
                        If Col_in_Excel < vFields.Length Then
                            TextBox2.Text += "cell " + vLabel(iLbl) + "= " + vFields(Col_in_Excel) + " "
                        End If
                    Next
                    TextBox2.Text += vbCrLf
                End If

            Next
        Catch ex As Exception
            MessageBox.Show("Error : " & ex.Message)
        End Try
    End Sub
commented: Thanks a lot for your help. +0
commented: Yes i try, its working. . Thanks guys. +3

I tried using this code and it worked. Thanks guys.

Thanks for helping with this question!

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.