繁体   English   中英

我的For和If陈述出现问题

[英]Having trouble with my For and If Statements

我正在尝试做一些事情,单击搜索按钮,它会在10,000个项目的工作表中搜索单元格“ A1”的值,然后使用搜索框将我希望从该信息行中获取的所有数据填充到工作表中。 我遇到的问题是,当我搜索一个项目并将其填充到应有的行时,然后我去搜索另一个项目,它将覆盖第一个项目。 我想要它,所以它将向下移动一行,因此当我搜索第二,第三甚至第四项时,它们都位于不同的行中。 我将在这里包含我的代码,所以请您帮忙。 我已经尝试过使用“偏移”功能,但仍然没有成功。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM