0

好的,所以我在 excel 中有一个用户表单,并且表单上有一个名为“IMEITextBox”的文本框。我有一张库存表,在 Coloum B 中有 IMEI 编号,我有库存。当我在 IMEITextBox 中输入 IMEI 号码时,我希望它在保存时从工作表“库存”中删除包含该 IMEI 号码的行。我已经研究了好几天了。似乎找不到任何对我有用的东西。你能帮忙吗?

Sub DeleteRows(IMEI)

Dim ws As Worksheet
Dim lastRow As Long, i As Long
Dim strSearch As String
Dim aCell As Range

On Error GoTo Err
Set ws = Sheets("Inventory")
lastRow = ws.Range("IMEIRange" & Rows.Count).End(xlUp).Row
strSearch = IMEITextBox.Value
Set aCell = ws.Range("IMEIRange" & lastRow).Find(What:=strSearch, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
  ws.Rows(lastRow).Delete
End If
Exit Sub
Err:
MsgBox Err.Description

End Sub
4

1 回答 1

1

我已经对代码进行了注释,以便您理解它不会有问题......看到代码后,您会意识到您非常接近;)

Option Explicit

Sub DeleteRows()
    Dim ws As Worksheet
    Dim strSearch As String
    Dim aCell As Range

    On Error GoTo Err

    '~~> Set the sheet where you want to search the IMEI
    Set ws = Sheets("Inventory")

    With ws
        '~~> Get the value which you want to search
        strSearch = IMEITextBox.Value

        '~~> Column A is Column 1 so Column B is 2. This is where we are searching
        '~~> xlWhole is used in the code below so that we find a complete match
        '~~> xlPart is supposed to be used when you are finding a partial match.
        Set aCell = .Columns(2).Find(What:=strSearch, LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

        '~~> Check if we found the value
        If Not aCell Is Nothing Then
            '~~> get the row of the cell where we found the match and delete it
            .Rows(aCell.Row).Delete
        Else '<~~ If not found
            MsgBox "IMEI Number not Found"
        End If
    End With

    Exit Sub
Err:
    MsgBox Err.Description
End Sub
于 2012-06-29T21:02:40.597 回答