出现运行时错误13- VBA Excel宏

时间:2019-02-27 14:12:15

标签: excel vba

enter image description here您好,我有一个Excel文件。 excel文件中有一个宏,用于清除相关的下拉列表。当我们将值从一行复制到另一行或将一列复制到另一列时,我们得到运行时错误13。请您帮助我们解决问题

代码:

Private Sub Worksheet_Change(ByVal Target As Range)
           If Target.Row > 2 And Target.Row < 100 Then

   If Target.Column = 2 And Target.Cells.Count = 1 And Target.Value = "Create Account_Personal" Then   
    Target.Offset(0, 3).Value = "NA"
   Target.Offset(0, 4).Value = "NA"
   Target.Offset(0, 5).Value = "NA"
   Target.Offset(0, 6).Value = "NA"
   Target.Offset(0, 7).Value = "NA"
   Target.Offset(0, 8).Value = "NA"
   Target.Offset(0, 9).Value = "NA"
   Target.Offset(0, 10).Value = "NA"
   Target.Offset(0, 11).Value = "NA"
     Target.Offset(0, 19).Value = "NO"
   Target.Offset(0, 22).Value = "NO THANKS"

      ElseIf Target.Column = 2 And Target.Cells.Count = 1 And Target.Value <> "Create Account_Personal" Then
      Target.Offset(0, 3).Value = "Select Country"

End If
   If Target.Column = 5 And Target.Cells.Count = 1 And Target.Value <> "NA" Then
   Target.Offset(0, 1).Value = "select State"
   Target.Offset(0, 2).Value = ""
   Target.Offset(0, 3).Value = ""
   Target.Offset(0, 4).Value = ""
   Target.Offset(0, 5).Value = ""
   Target.Offset(0, 6).Value = ""
   Target.Offset(0, 7).Value = ""
   Target.Offset(0, 9).Value = ""
   Target.Offset(0, 10).Value = ""
   Target.Offset(0, 11).Value = ""
   Target.Offset(0, 12).Value = ""
   Target.Offset(0, 13).Value = ""
   Target.Offset(0, 14).Value = ""
   Target.Offset(0, 15).Value = ""
  End If

End If
End Sub`enter code here`

image

1 个答案:

答案 0 :(得分:1)

  1. 使用Application.EnableEvents = False
  2. 禁用事件
  3. 对于连续的单元格,请使用...

    Target.Parent.Range(Target.Offset(0, 3), Target.Offset(0, 11)).Value = "NA"
    

    …在第3列和第11列之间写NA。这更短,更快。


Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    On Error GoTo ENABLE_EVENTS

    If Target.Row > 2 And Target.Row < 100 Then
        If Target.Column = 2 And Target.Cells.Count = 1 Then
            If Target.Value = "Create Account_Personal" Then
                Target.Parent.Range(Target.Offset(0, 3), Target.Offset(0, 11)).Value = "NA"
                Target.Offset(0, 19).Value = "NO"
                Target.Offset(0, 22).Value = "NO THANKS"
            ElseIf Target.Value <> "Create Account_Personal" Then
                Target.Offset(0, 3).Value = "Select Country"
            End If
        End If

        If Target.Column = 5 And Target.Cells.Count = 1 Then
            If Target.Value <> "NA" Then
                Target.Offset(0, 1).Value = "select State"
                Target.Parent.Range(Target.Offset(0, 2), Target.Offset(0, 7)).Value = ""
                Target.Parent.Range(Target.Offset(0, 9), Target.Offset(0, 15)).Value = ""
            End If
        End If
    End If

ENABLE_EVENTS:
    Application.EnableEvents = True
    If Err.Number <> 0 Then Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
End Sub