当同时删除多个单元格时,我收到运行时错误13

时间:2014-12-29 08:16:22

标签: excel vba excel-vba

我得到了以下代码,只选择一个单元格并删除它时工作正常,但是当我选择多个单元格时,我得到运行时错误13"类型不匹配":

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim sheetName As String
    Dim sheetCodeName As String
    Dim cel As Range

    If Target.Column = "12" Then
    On Error GoTo DELETESTUFF
        sheetName = Target.Value
    On Error GoTo 0
        sheetCodeName = Target.Offset(0, -2).Value

        For Each Sheet In ActiveWorkbook.Worksheets
            If Sheet.CodeName = sheetCodeName Then
            If sheetName = "" Then
                On Error GoTo INVALIDCOLUMNNAME:
            End If
                On Error GoTo INVALIDCOLUMNNAME:
                Sheet.Name = sheetName
                On Error GoTo 0

            End If

        Next Sheet

    End If

Exit Sub

INVALIDCOLUMNNAME:

If Target.Column = "12" Then
    sheetName = Target.Offset(0, -1).Value
    sheetCodeName = Target.Offset(0, -2).Value

    For Each Sheet In ActiveWorkbook.Worksheets
        If Sheet.CodeName = sheetCodeName Then

            On Error GoTo INVALIDCO:
            Sheet.Name = sheetName
            On Error GoTo 0

        End If

    Next Sheet

End If

Exit Sub

DELETESTUFF:

If Not Intersect(Target, Columns(12)) Is Nothing Then
    On Error GoTo INVALIDCO
    Application.EnableEvents = False
    For Each cel In Intersect(Target, Columns(12))
        sheetName = cel.Offset(0, -1).Value
        sheetCodeName = cel.Offset(0, -2).Value
        For Each Sheet In ThisWorkbook.Worksheets
            If Sheet.CodeName = sheetCodeName Then
                Sheet.Name = sheetName
                Exit For  'no need to continue
            End If
        Next Sheet
    Next cel
End If

Exit Sub

INVALIDCO:
Application.EnableEvents = True

有人可以帮忙吗?

提前谢谢

1 个答案:

答案 0 :(得分:0)

您使用Target"删除" 告诉我这是一个Worksheet_Change事件宏。在这些情况下,Target可以代表一系列细胞而不仅仅是一个细胞。您需要For Each ... In Intersect(Target, Columns(12)遍历每个匹配的单元格,并执行与每个单元格相关的操作

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Columns(12)) Is Nothing Then
        On Error GoTo INVALIDCO
        Application.EnableEvents = True
        Dim cel As Range, sheetName As String, sheetCodeName As String, ws As Worksheet
        For Each cel In Intersect(Target, Columns(12))
            sheetName = cel.Offset(0, -1).Value
            sheetCodeName = cel.Offset(0, -2).Value
            For Each ws In ThisWorkbook.Worksheets
                If ws.CodeName = sheetCodeName Then
                    If IsEmpty(cel) Then
                        ws.Name = Right("0" & Replace(ws.CodeName, "sheet", vbNullString, 1, 1, vbTextCompare), 2)
                    ElseIf CBool(Len(sheetName)) Then
                        'addition worksheet name validation here
                        ws.Name = sheetName
                    Else
                        'do nothing. blank worksheet name
                    End If
                    Exit For
                End If
            Next ws
        Next cel
    End If
INVALIDCO:
    Application.EnableEvents = True
End Sub

换句话说,如果您选择了L2:L5并点击了删除,那么Target等于L2:L5,您无法将K2:K5分配给 sheetName 您在原始代码中的方式。如果您计划将其用于倍数,则应添加其他错误控制,以确保列L中每个已删除值的J和K列中都有适当的值。