如何删除 Excel Table 中的一行,该行位于另一个 Sheet 中,该行是使用 VBA 通过用户表单搜索字段找到的?

How do I delete a row in an Excel Table that is on another Sheet that was found with a Userform search field using VBA?

我的第一个 Sub 使用我在用户表单中输入的值在另一个 sheet 上搜索 table 并使用当前输入的值更新用户表单 sheet。

我有第二个 Sub,它将更新后的值写回到同一个 sheet。我有一个复选框,当相关项目完成并且我们不再需要该行中的数据并希望将其删除时,用户会选中该复选框。

选中此项并且用户单击更新时,子项将从 sheet 中删除该行。

主要 sheet 只是包含实际数据的 sheet 的副本。 sheet 从 Zapier 接收更新。我有隐藏行和列的宏,这会在连接的应用程序尝试添加数据时造成混乱,这是第二个 sheet.

的原因

我今天花了一整天时间想弄清楚如何删除已选中的行 通过用户表单中的搜索功能。 sub 在更新行时执行相同的搜索,所以我想如果我添加一个 IF 语句来根据复选框的状态更新或删除。

这是我的代码:

Private Sub UpdateRecord()

     Dim RecordRow As Long
     Dim RecordRange As Range
     Dim Answer As VbMsgBoxResult
     Dim DeleteRow As Long
     Dim DeleteRange As Range
     Dim ws As String
     
      
        ' Find the row in the table that the record is in
        RecordRow = Application.Match(CLng(TextBoxWO.Value), Range("JobSheetData[W/O]"), 0)
        
        ' Set RecordRange to the first cell in the found record
        Set RecordRange = Range("JobSheetData").Cells(1, 1).Offset(RecordRow - 1, 0)
        
        If CheckBoxDelete = "True" Then
        
'---------------------True = Delete-------------------
        
       ' Find the row in the table that the record is in
        DeleteRow = Application.Match(CLng(TextBoxWO.Value), Range("JobSheetData[W/O]"), 0)

        ' Set RecordRange to the first cell in the found record
        Set DeleteRange = Range("JobSheetData").Cells(1, 1).Offset(RecordRow - 1, 0)
        

        Answer = MsgBox("Are you sure you want to PERMANENTLY DELETE this job from the job list?" & vbCrLf & vbCrLf & "This action cannot be undone!", vbOKCancel + vbDefaultButton2, "Confirm removal of job from list")

        If Answer = vbYes Then
        
        ActiveWorkbook.Worksheets("Job List Import").ListObjects("JobSheetData").ListRows(DeleteRange).Delete
        

        End If

'---------------------False = Update-------------------
        
        Else
        
            RecordRange(1, 1).Offset(0, 5).Value = TextBoxHold.Value
            RecordRange(1, 1).Offset(0, 7).Value = TextBoxDays.Value
            RecordRange(1, 1).Offset(0, 9).Value = CheckBoxLocate.Value
            RecordRange(1, 1).Offset(0, 13).Value = TextBoxFirst.Value
            RecordRange(1, 1).Offset(0, 14).Value = TextBoxOveride.Value
            RecordRange(1, 1).Offset(0, 15).Value = CheckBoxBell.Value
            RecordRange(1, 1).Offset(0, 16).Value = CheckBoxGas.Value
            RecordRange(1, 1).Offset(0, 17).Value = CheckBoxHydro.Value
            RecordRange(1, 1).Offset(0, 18).Value = CheckBoxWater.Value
            RecordRange(1, 1).Offset(0, 19).Value = CheckBoxCable.Value
            RecordRange(1, 1).Offset(0, 20).Value = CheckBoxOther1.Value
            RecordRange(1, 1).Offset(0, 21).Value = CheckBoxOther2.Value
            RecordRange(1, 1).Offset(0, 22).Value = CheckBoxOther3.Value
        
        End If
             
End Sub

我也尝试合并此代码,但也无法使其正常工作:


    Dim Answer As VbMsgBoxResult
    Answer = MsgBox("Are you sure you want to PERMANENTLY DELETE this job from the job list?" & vbCrLf & vbCrLf & "This action cannot be undone!", vbOKCancel + vbDefaultButton2, "Confirm removal of job from list")

        If Answer = vbYes Then
        
        If JobSheetInputForm.ListRows.Count < 1 Then Exit Sub
    
        With ActiveSheet.ListObjects("JobSheet")
        
            JobSheetInputForm.ListRows(CurrentRow).Delete
                        
            If JobSheetInputForm.ListRows.Count > 0 Then
            
                If CurrentRow > JobSheetInputForm.ListRows.Count Then
                
                    CurrentRow = JobSheetInputForm.ListRows.Count
                    
                End If
                
                JobSheetInputForm.ListRows(CurrentRow).Range.Select
            
            Else
            
                CurrentRow = 0
            
            End If
                        
        End With

    End If

我现在很沮丧,需要你的帮助!谢谢!

像这样:

Sub Test()
    
    Dim rngSrch As Range, lo As ListObject, DeleteRow, v
    
    Set lo = ActiveWorkbook.Worksheets("Job List Import").ListObjects("JobSheetData")
    
    Set rngSrch = lo.ListColumns("W/O").DataBodyRange 'range to search in
    v = CLng(TextBoxWO.Value)                         'value to search
    
    DeleteRow = Application.Match(v, rngSrch, 0)      'try to match a row
    If Not IsError(DeleteRow) Then                    'got match?
        If MsgBox("Are you sure you want to PERMANENTLY DELETE this job from the job list?" & _
                   vbCrLf & vbCrLf & "This action cannot be undone!", _
                   vbOKCancel + vbDefaultButton2, "Confirm removal of job from list") = vbOK Then
                   
                   lo.ListRows(DeleteRow).Delete
        
        End If
    Else
        MsgBox "Value not found!" 'no match
    End If

End Sub