简体   繁体   English

我的For和If陈述出现问题

[英]Having trouble with my For and If Statements

I am trying to make something where I click my search button and it searches the value of cell "A1" in a sheet of 10,000 items and then populates all the data that I want from that row of information into the sheet with my searchbox. 我正在尝试做一些事情,单击搜索按钮,它会在10,000个项目的工作表中搜索单元格“ A1”的值,然后使用搜索框将我希望从该信息行中获取的所有数据填充到工作表中。 The problem I am encountering is that when I search an item and it populates the row it is supposed to, then I go to search for another item, it will just overwrite the first one. 我遇到的问题是,当我搜索一个项目并将其填充到应有的行时,然后我去搜索另一个项目,它将覆盖第一个项目。 I want it so it will move down a line so when I go to search a 2nd,3rd even 4th item they are all in different rows. 我想要它,所以它将向下移动一行,因此当我搜索第二,第三甚至第四项时,它们都位于不同的行中。 I will include my code here so please if you can help that'd be great. 我将在这里包含我的代码,所以请您帮忙。 I have tried using Offset and still to no prevail. 我已经尝试过使用“偏移”功能,但仍然没有成功。

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

Your incrementation is wrong: 您的增量是错误的:

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