2017-06-23 68 views
0

我試圖做一些事情,我點擊我的搜索按鈕,它在一張10000個項目中搜索單元格A1的值,然後填充我想要的所有數據我的搜索框將一行信息放入表格中。我遇到的問題是,當我搜索一個項目並填充它應該設置的行時,我會去搜索另一個項目,它會覆蓋第一個項目。我想要它,所以它會向下移動,所以當我去搜索第二,第三甚至第四項時,它們都在不同的行中。我會在這裏包括我的代碼,所以如果你能提供幫助,那就太好了。我曾嘗試使用膠印,但仍然沒有佔上風。遇到我的For和If語句有問題

Sub SearchBox() 

Dim erow As Long 
Dim ws As Worksheet 
Dim lastrow As Long 
Dim count As Integer 

lastrow = Sheets("Charlotte Gages").Cells(Rows.count, 1).End(xlUp).Row 

For x = 2 To lastrow 
i = 3 
If Sheets("Charlotte Gages").Cells(x, 1) = Sheets("Gages").Range("A1") Then 
Sheets("Gages").Cells(i, 1) = Sheets("Charlotte Gages").Cells(x, 1) 
Sheets("Gages").Cells(i, 2) = Sheets("Charlotte Gages").Cells(x, 2) 
Sheets("Gages").Cells(i, 3) = Sheets("Charlotte Gages").Cells(x, 3) 
Sheets("Gages").Cells(i, 4) = Sheets("Charlotte Gages").Cells(x, 4) 
Sheets("Gages").Cells(i, 5) = Sheets("Charlotte Gages").Cells(x, 5) 
Sheets("Gages").Cells(i, 6) = Sheets("Charlotte Gages").Cells(x, 6) 
count = count + 1 
If Sheets("Charlotte Gages").Cells(x, 1) = "Found" Then 
    i = 3 + 1 

End If 

Next x 


If count = 0 Then 
MsgBox ("Cannot Find Gage, Please check Gage ID") 

End If 

End Sub 

回答

3

你的增量是錯誤的:

Sub SearchBox() 
Dim lastrow As Long 
dim i as long, x as long 

lastrow = Sheets("Charlotte Gages").Cells(Rows.count, 1).End(xlUp).Row 
i = 3 
For x = 2 To lastrow 

    If Sheets("Charlotte Gages").Cells(x, 1) = Sheets("Gages").Range("A1") Then 
     Sheets("Gages").Cells(i, 1).Resize(,6).Value = Sheets("Charlotte Gages").Cells(x, 1).Resize(,6).Value    
     i = i + 1 
    End If 

Next x 


If i = 3 Then 
    MsgBox ("Cannot Find Gage, Please check Gage ID")  
End If 

End Sub 
+0

非常感謝您! – Jmaragno

+1

@Jmaragno如果這項工作成功,請點擊答案旁邊的複選標記標記爲正確。 –

+0

出於某種原因,當我在搜索中輸入新的內容時,它仍然只是覆蓋前一項 – Jmaragno