我的 For 和 If 语句有问题

杰马拉尼奥

我试图在我单击搜索按钮的地方做一些事情,它在 10,000 个项目的工作表中搜索单元格“A1”的值,然后用我的搜索框将我想要的那一行信息中的所有数据填充到工作表中。我遇到的问题是,当我搜索一个项目并填充它应该填充的行时,然后我去搜索另一个项目,它只会覆盖第一个。我想要它,所以它会向下移动,所以当我去搜索第二、第三甚至第四个项目时,它们都在不同的行中。我将在此处包含我的代码,所以如果您能提供帮助,那就太好了。我曾尝试使用 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
斯科特·克雷纳

你的增量是错误的:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章