简体   繁体   English

Excel VBA-如何在列中查找值并返回其所在的行

[英]Excel VBA - How to find a value in a column and return the row it is on

I currently use a for loop to look through column A in the HDD database sheet and when it finds the matching search criteria it copies all the information in that row into a designated area. 我目前使用for循环浏览HDD数据库工作表中的A列,当找到匹配的搜索条件时,它将该行中的所有信息复制到指定区域中。

I am trying to use the same for loop to tell me which row the search criteria is found on so i can delete that row from the HDD database sheet. 我试图使用相同的for循环来告诉我搜索条件所在的行,这样我就可以从HDD数据库工作表中删除该行。

Tried a few things but nothing seems to work. 尝试了几件事,但似乎无济于事。

Any help is very welcome on this. 任何帮助都非常欢迎。

Private Sub EditButton_Click()

    Dim Userentry As String
    Dim i As Long
    Dim ws, ws1, ws2 As Worksheet
    'Dim found As Range
    Dim RowNumber As String
    Set ws = Sheets("HDD database")
    Set ws1 = Sheets("HDD log")
    Set ws2 = Sheets("Sheet3")


    Userentry = editTxtbox.Value
    'ws1.Range("A36").Value = Userentry

    For i = 1 To ws.Range("A" & Rows.Count).End(xlUp).Row
        If (ws.Cells(i, 1).Value) = Userentry Then
            ws2.Range("A1").Offset(1, 0).Resize(1, 8).Value = _
                      ws.Cells(i, 1).Resize(1, 8).Value
        End If

    Next i

    addnewHDDtxtbox.Value = ws2.Range("A2")
    MakeModeltxtbox.Value = ws2.Range("B2")
    SerialNumbertxtbox.Value = ws2.Range("C2")
    Sizetxtbox.Value = ws2.Range("D2")
    Locationtxtbox.Value = ws2.Range("E2")
    Statetxtbox.Value = ws2.Range("F2")


    For i = 1 To ws.Range("A" & Rows.Count).End(xlUp).Row
        If (ws.Cells(i, 1).Value) = Userentry Then
            RowNumber = ws.Cells.Row
        End If
    Next i

    ws1.Range("I3").Value = RowNumber

End Sub

Change: 更改:

RowNumber = ws.Cells.Row

to

RowNumber = i

I think this should help. 我认为这应该有所帮助。 Using Range.Find() should speed it up a little. 使用Range.Find()应该可以加快速度。 I can edit this if you want more, just comment. 如果您需要更多信息,我可以对其进行编辑,只需发表评论即可。

'I liked your found :)
Dim found As Range
'Set found equal to the cell containing your string
Set found = ws.Range("A:A").Find(Userentry)
'Show the row of found if you want
'MsgBox found.Row
'Delete found's row
'ws.found.Rows.Delete
'Alternately, set the value of I3 to found's row
ws1.Range("I3").Value = ws.found.row

暂无
暂无

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

相关问题 在excel vba中返回绝对值的行和列 - return the row and column of the absolute value in excel vba 如何在excel VBA中根据行和列ID查找单元格值 - How to find cell value based on row and Column ID in excel VBA Excel VBA - 在表列中查找给定值的行号 - Excel VBA - Find Row Number for Given Value in a Table Column Excel VBA:查找带有日期的列,如果找到则返回列值 - Excel VBA: Find columns with dates, return column value if found Excel-如何将列标题与行标题(日期)匹配并在VBA中返回查找值 - Excel - how to match a column header with a row header (dates) and return a lookup value in VBA Excel VBA —查找列A中任何值的第一个出现位置,然后将列B的值插入列C(同一行) - Excel VBA — Find first occurrence of any value in column A, then insert value of column B into column C (same row) 如何通过 vba 代码 Cells.Find 在 excel 列中查找值 - How to find a value in an excel column by vba code Cells.Find 一个程序,它将在最后一行中找到具有最小值的列,并将其在第一行中的值分配给变量? [Excel VBA] - A program that will find the column with a minimum value in its last row and assign its value in the first row to a variable? [Excel VBA] Excel Vba如何在for循环中找到行的最小值 - Excel Vba how to find row's minimum value in for loop 在工作表1的G列中找到非零值,将该行中的列C的值返回到工作表2(VBA) - Find non zero value in column G on sheet 1, return value of Column C in that row to sheet 2 (VBA)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM