循环遍历excel行

时间:2015-06-30 17:05:35

标签: vb.net excel

我正在尝试使用VB.Net循环遍历Excel数据。我想要的是VB在Excel中循环B列并在textbox1.text中找到匹配的值,一个消息框应该说“你好”。

我已经能够使VB.Net打开Excel和我的工作簿,但是当我运行代码时,它给我一个错误

if .Cells(i, 1)=LowerCode Then

我被困在这几个小时。请提出任何建议或想法。以下是我的代码:

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

1 个答案:

答案 0 :(得分:0)

你应该循环遍历行并查找像这样的索引单元格值

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