2015-06-30 110 views
0

我想使用VB.Net循環通過Excel數據。我想要的是VB在Excel中循環遍歷B列,並在textbox1.text中找到匹配的值,並在消息框中顯示「hello」。通過excel行循環

我已經能夠使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 
+0

該索引的單元格值你有選擇上嚴格?編號建議它。你試過「如果.Cells(i,1).Value = LowerCode」 – majjam

+0

http://stackoverflow.com/questions/2454552/whats-an-option-strict-and-explicit – majjam

+0

什麼是錯誤?你有沒有試過闖入代碼,看看你正在看的單元格有什麼價值? –

回答

0

你應該通過行循環,並在尋找這樣

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