简体   繁体   English

如何删除 Excel 表中的一行,该表位于使用 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?

My first Sub uses a value that I enter in a userform to search a table on another sheet and update the userform with the currently entered values from that sheet.我的第一个 Sub 使用我在用户表单中输入的值来搜索另一张工作表上的表格,并使用该工作表中当前输入的值更新用户表单。

I have a second Sub that will write the updated values back to the same sheet.我有第二个 Sub,它将更新后的值写回同一张表。 I have a checkbox that the user checks when the the related project is complete and we no longer need the data in the row and would like it deleted.我有一个复选框,当相关项目完成并且我们不再需要该行中的数据并希望将其删除时,用户会选中该复选框。

用户表单

When this is checked and the user clicks update, the sub is to delete that Row from the sheet.检查此操作并单击“更新”时,该行是从表中删除该行。

The main sheet is just a replica of the sheet that contains the actual data.主工作表只是包含实际数据的工作表的副本。 That sheet receives updates from Zapier.该工作表从 Zapier 接收更新。 I have macros that hide rows and columns and this messes with connected apps when they try to add data which is the reason for the second sheet.我有隐藏行和列的宏,当连接的应用程序尝试添加数据时,这会弄乱它们,这就是第二张表的原因。

I have spent all day today trying to figure out how to delete the row that has been selected by the search function in the userform.我今天一整天都在想办法删除用户窗体中搜索 function 选择的行。 The sub performs the same search when updating the row so I thought if I added an IF statement to either update or delete depending on the status of the checkbox. sub 在更新行时执行相同的搜索,所以我想如果我添加一个 IF 语句来根据复选框的状态更新或删除。

Here is my code as it stands:这是我的代码:

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

I also tried to incorporate this code but could not get it to work either:我也尝试合并此代码,但也无法使其正常工作:


    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

I am beat at this point and need your Help!我在这一点上被打败了,需要你的帮助! Thank you!谢谢!

Something like this:是这样的:

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

暂无
暂无

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

相关问题 如何使用Java删除excel(csv)表中的行? - How do I delete a row in an excel (csv) sheet using Java? 如何在Excel表格中搜索文本,然后删除整个行? - How do I search a excel table for a text and then delete that entire row? 如何从单独的 excel 表 VBA 中删除最后一行 - How do i delete the last row from a separate excel sheet VBA Excel VBA二进制搜索以将一个工作表中的列与另一个工作表中的列进行比较,如果匹配则删除整行 - Excel VBA binary search to compare columns in one sheet to columns in another and delete the entire row if they match 使用VBA将行复制到excel中的另一个工作表 - Copy row to another sheet in excel using VBA 如何使用另一列中的值搜索子字符串,然后删除行,Excel VBA - How to search a substring using values from another column and then delete row, Excel VBA 如果在其他工作表的列中未找到单元格值,则Excel VBA删除行 - Excel VBA delete row if cell value not found in a column in other sheet Excel 2007 VBA:如何从一张纸上的动态范围复制和粘贴到另一张纸的第一行? - Excel 2007 VBA: How do I copy and paste from a dynamic range on one sheet to the first empty row of another sheet? 使用VBA excel从一张工作表中复制表格并将其粘贴到另一张工作表的下一个空行中 - copy a table from one Sheet and paste it in the next empty row of another Sheet using VBA excel 如何使用 VBA 将 excel 表中的信息输入到特定网站的搜索栏? - How do I enter information from my excel sheet to a specific website's search bar using VBA?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM