需要用户表单删除功能的帮助

需要用户表单删除功能的帮助

我可以删除第一张工作表中的数据,但对于第 5 张工作表却不行。我没有遇到任何错误。我该如何修复这个问题?

'Delete Function'
  Private Sub Delete_Click()
    Row = 3
    Dim lastrow As Long
    Dim StaffID As String
    lastrow = Sheets("Staff Details").Range("A" & Rows.Count).End(xlUp).Row

   StaffID = TextBox6.Text
   For Row = 3 To lastrow
   answer = MsgBox("Are you sure you wish to delete the Staff Record?", vbYesNo + vbQuestion, "Delete Staff Record")
   If answer = vbYes And Cells(Row, 1).Text = StaffID Then
    Cells(Row, 1).EntireRow.Delete

   ElseIf Cells(Row, 1).Text = StaffID Then
    Sheet5.Activate
    Cells(Row, 1).EntireRow.Delete

   End If
      Next Row
      TextBox6.SetFocus

End Sub

答案1

如果在输入框中写入的 A 列中找到 ID,此宏将帮助您从任何工作表中删除记录。

Sub DeleteAllRow()
Dim strTodel As String
Dim colTofind As String
Dim delRows As Long
strTodel = InputBox("Enter the ID")
Do While True
 On Error GoTo Err_handle
 Range("A:A").Find(What:=strTodel, LookIn:=xlFormulas, LookAt:=xlWhole, After:=ActiveCell).EntireRow.Delete Shift:=xlUp
 delRows = delRows + 1
Loop
Err_handle:
 MsgBox "Number of Delete Rows - " & delRows
 Exit Sub
End Sub

怎么运行的:

  • 复制并粘贴此代码作为标准模块,同时单击This Workbook Icon in VBA Project Explorer Windows
  • 使用此功能Workbook's Macro将帮助您从任何工作表中删除记录。
  • Range("A:A")是可编辑的,您可以设置任意列来搜索ID来删除记录。

相关内容