好的,我在excel中有一个userform,我在表单上有一个名为“IMEITextBox”的文本框。我有一个库存表,在库拉姆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
答案 0 :(得分: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