[英]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.