遇到我的For和If语句有问题

我试图做一些事情,我点击我的searchbutton,它search单元格中的值为“10000”的10,000个项目,然后填充我想从该行信息到我的search框的工作表中的所有数据。 我遇到的问题是,当我search一个项目,它填充行应该,然后我去search另一个项目,它只会覆盖第一个。 我想要它,所以它会下移一行,所以当我去search第二,第三甚至第四个项目,他们都在不同的行。 我会在这里包括我的代码,所以如果你能帮到的话,那就太好了。 我已经尝试使用偏移量,仍然没有占上风。

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 

你的增量是错误的:

 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