有谁能够提供一些指导,告诉我如何最好地编写一个代码,让我能够在我的表中删除所选的行?在我的表中有隐藏的公式,链接到其他单元格& 工作表以及隐藏的列。这是我目前正在修改的代码,它可以删除行,但是当运行代码时,表格公式会返回一个 #REF!
错误。相当多的新手,所以任何帮助和指导是非常感激! :)
Sub Remove_Selected_Data()
' This subroutine clears the Selected Data from the row in the expenses and income table
' which is currently selected
Dim iSelectedRow As Integer
Dim sAnswer As String
Dim wb As ThisWorkbook
Dim ws As Worksheet
Dim lo As ListObject
Set wb = ThisWorkbook
Set ws = ThisWorkbook.Worksheets("2020_Data")
Set lo = ThisWorkbook.Worksheets("2020_Data").ListObjects("Table1")
Application.ScreenUpdating = False
iSelectedRow = ActiveCell.Row
' Check if selected row is a valid row to delete
If iSelectedRow = 1 Then
MsgBox "Please select a row within the Table!", vbExclamation, "Test1"
Exit Sub
ElseIf iSelectedRow > lo.DataBodyRange.Rows.Count + 1 Then
MsgBox "Please select a row within the Table!", vbExclamation, "Test2"
Exit Sub
End If
' Check if you really want to clear all data
sAnswer = MsgBox("Are you sure you want to clear the Selected data?" & vbNewLine & _
"Click Yes to continue", vbYesNo + vbQuestion, "Test3")
If sAnswer = vbYes Then
Application.EnableEvents = False
ws.Rows(iSelectedRow).Delete
Application.EnableEvents = True
End If
Application.ScreenUpdating = True
End Sub
先谢谢你的帮助! :)
看来表内单元格中的一些公式是引用了前面的行。试着用.Filldown之类的方法把删除行后的错误列中的公式填下来。