Sotie
Sotie

Reputation: 11

Loop through excel rows

I am trying to use VB.Net to loop through an Excel data. What I want is for VB to loop through column B in Excel and find matching values in textbox1.text and a message box should say "hello".

I have been able to make VB.Net open Excel and my Workbook, but when I run the code, it gives me an error at

if .Cells(i, 1)=LowerCode Then

I have been stuck on this for hours. Please any suggestions or ideas would be appreciated. Below is my code:

Dim LowerCode As Integer
Dim FinalRow As Integer
Dim i As Integer

LowerCode = TextBox1.Text
FinalRow = xlWorkSheet.Range("B30").End(Excel.XlDirection.xlUp).Row

With xlWorkSheet
    For i = 2 To FinalRow
        If .Cells(i, 1) = LowerCode Then
            MsgBox("Hello")
        End If
    Next i

End With

Upvotes: 1

Views: 8080

Answers (1)

Grant Wilson
Grant Wilson

Reputation: 23

should you be looping through the rows and looking for that indexed cell value like this

   for i= 2 to finalrow
       if xlWorkSheet.rows(i).cells(1).text = Lowercode then
          msgbox()
       end if
   next i

Upvotes: 1

Related Questions