excel - Selection.EntireRow.Delete 错误,可以在行的多列中选择单元格?

标签 excel vba error-handling

我需要删除模板中的选定行。Selection.EntireRow.Delete处理我所有的场景,但只有一个。
当我选择同一行时,在多个选择中Selection.EntireRow.Delete将抛出 1004 错误。
我写了一个 ErrorHandler 来警告这种情况,然后继续不让模板解锁。

Sub DelRows()

    Dim answer As VbMsgBoxResult, LRow As Integer
    LRow = Range("D5").Value
    
    If Selection.Cells(1, 1).Row < 9 Or Selection.Cells(1, 1).Row + Selection.Rows.Count > LRow - 1 Then                                            
        MsgBox "Forbidden selection."
    ElseIf Selection.Rows.Count > LRow - 11 Then                                                                                                    
        MsgBox "Can't delete all rows."
    Else
        On Error GoTo ErrHandler
        ActiveSheet.Unprotect Password:="xx"
        answer = MsgBox("Are you sure you want to delete all rows?", vbOKCancel + vbExclamation, "Warning")
        If answer = 1 Then Selection.EntireRow.Delete                        
        Rows((LRow + 2) & ":1048576").EntireRow.Hidden = True                               
        ActiveSheet.Protect Password:="xx", DrawingObjects:=False, Contents:=True, Scenarios:=True, AllowFormattingCells:=True, AllowFormattingRows:=True, AllowFiltering:=True
    
    End If
    Exit Sub

    ErrHandler:
        If Err.Number = 1004 Then
            MsgBox "Can't select the same row twice: " & Err.Description
            Resume Next
        Else
            MsgBox Err.Description
            Resume Next
    End If
End Sub
这件事有效,但我在错误处理方面的经验并不是最好的。
我可以用其他方法避免这个错误吗?
我可以检查同一行中的多个选择,还是不值得这样做?
我可能会循环所有选定的单元格,创建一个包含每个区域的行的数组,然后比较结果,但这似乎太过分了。
--最终解决方案--
感谢 Pᴇʜ我设法不仅解决了这个问题,而且同时找到并解决了另外两个问题。
多项选择打破了我的Selection.Cells(1, 1).RowSelection.Rows.Count因为他们只关心第一个区域。这两者都可以通过循环区域来修复。
    Dim answer As VbMsgBoxResult, LRow As Long, i As Long, RowCount As Long, TopRow As Long
    LRow = Range("D5").Value
    TopRow = Selection.Cells(1, 1).Row
    
    For i = 1 To Selection.Areas.Count
        RowCount = RowCount + Selection.Areas(i).Rows.Count
        If Selection.Areas(i).Cells(1, 1).Row < TopRow Then TopRow = Selection.Areas(i).Cells(1, 1).Row
    Next i
    
    If TopRow < 9 Or TopRow + RowCount > LRow - 1 Then                                         
        MsgBox "Forbidden selection."
    ElseIf RowCount > LRow - 11 Then                                                                                                    
        MsgBox "Can't delete all rows."
    Else
        ActiveSheet.Unprotect Password:="xx"
        answer = MsgBox("Are you sure you want to delete all rows?", vbOKCancel + vbExclamation, "Warning")
        If answer = 1 Then Selection.EntireRow.Delete                        
        Rows((LRow + 2) & ":1048576").EntireRow.Hidden = True                               
        ActiveSheet.Protect Password:="xx", DrawingObjects:=False, Contents:=True, Scenarios:=True, AllowFormattingCells:=True, AllowFormattingRows:=True, AllowFiltering:=True
    
    End If
End sub

最佳答案

你可以做一个技巧并使用 Application.Union method

Union(Selection.EntireRow, Selection).delete

这样就避免了一行多选的错误。

根据评论编辑:
Dim i As Long, RowCount As Long
For i = 1 To Selection.Areas.Count
    RowCount = RowCount + Selection.Areas(i).Rows.Count
Next i

关于excel - Selection.EntireRow.Delete 错误,可以在行的多列中选择单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53596552/

相关文章:

if-statement - 您如何编写方法的错误检查部分以使其可读并易于出错?

excel - 当我已经使用 Workbooks.OpenText 打开它时关闭文本文件

excel - 冷融合11 : CFContent doesn't write openable excel file

java - 如何使用 POI 链接 Excel 文件中的图像

vba - 根据条件向上和向下填充多列中的单元格

mysql - PowerPoint幻灯片显示来自MySql数据库的数据?如何?

reactjs - Redux中的验证处理

Excel VBA - 保存前运行宏

vba - 如何在 MS Office 2013 中自动查找/替换 VBA 模块中的代码?

c - 使用函数返回值进行错误检查和/或异常处理是好的和适当的技术吗?