【发布时间】:2017-06-23 18:23:43
【问题描述】:
我正在尝试在单击搜索按钮的地方做一些事情,它会在包含 10,000 个项目的工作表中搜索单元格“A1”的值,然后用我的信息从该行信息中填充我想要的所有数据到工作表中搜索框。我遇到的问题是,当我搜索一个项目并填充它应该填充的行时,然后我去搜索另一个项目,它只会覆盖第一个项目。我想要它,所以它会向下移动一行,所以当我去搜索第 2、3 甚至第 4 项时,它们都在不同的行中。我将在此处包含我的代码,所以如果您能提供帮助,那就太好了。我尝试过使用 Offset,但仍然没有成功。
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
【问题讨论】:
标签: excel vba for-loop if-statement