vb.net 循环遍历excel行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31144229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:15:32  来源:igfitidea点击:

Loop through excel rows

vb.netexcel

提问by Sotie

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".

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

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

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

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

回答by Grant Wilson

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