我的VBA代码无法正常工作,错误消息“编译错误:无效的外部过程”

时间:2019-06-24 03:34:29

标签: excel vba

我正在创建一个为2019年编译的数据列表。我想在我的excel数据中消除不活跃的行,并将其移动到名为“不活跃(12个月)”的单独工作表中。我将活动列设置为A,在其中我将列出“非活动”或留空。

我将代码复制到新的excel工作表上,并尝试保存它,但是当我按下Alt-F8时,我也看不到保存的vba代码,它也无法运行。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
' Only react to edits in Column A:  '
    If Not Intersect(Target, Sheets("Buyer Limit").Range("A:A")) Is Nothing Then
    ' Dont do anything if > 1 cell was just changed:   '
    If Target.Cells.Count = 1 Then
        ' Only make the change if the new value in Col A is "inactive":    '            If Target.Value = "Inactive" Then
            ' Find the next available cell on the Inactive(12mths) sheet for a name:   '
            Dim nextRange As Range
            Set nextRange = Sheets("Inactive(12mths").Range("A65536").End(xlUp).Offset(1, 0)
            ' Cut the employee name and status and paste onto the Inactive(12mths) sheet:   '
            Range(Target, Target.Offset(0, -1)).Cut
            Sheets("Buyer Limit").Paste Destination:=Sheets("Inactive(12mths").Range(nextRange.Address)
        End If
    End If
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub

我希望在打开工作表时此vba代码的输出会自动运行,但不会自动运行。我不确定是否保存错误。 =(

19年6月25日(更新)

我已经改写了报价,但是仍然无法在我的Excel表格(启用了宏的表格)上使用它。

Private Sub Worksheet_Activate(ByVal Target As Range)
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
    ' Only react to edits in Column A:  '
    If Not Intersect(Target, Sheets("Buyer").Range("A:A")) Is Nothing Then
        ' Dont do anything if > 1 cell was just changed:   '
        If Target.Cells.Count = 1 Then
            ' Only make the change if the new value in Col A is "Inactive":    '
            If Target.Value = "Inactive" Then
                ' Find the next available cell on the Inactive sheet for a name:   '
                Dim nextRange As Range
                Set nextRange = Sheets("Inactive").Range("A65536").End(xlUp).Offset(1, 0)
                ' Cut the CP name and status and paste onto the Inactive sheet:   '
                Range(Target, Target.Offset(0, -1)).Cut
                Sheets("Buyer").Paste Destination:=Sheets("Inactive").Range(nextRange.Address)
        End If
    End If
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub

1 个答案:

答案 0 :(得分:0)

您发布的代码中似乎没有任何东西可以解释“无效的外部过程”。您还有尚未发布的其他代码吗?

类似的事情应该起作用:

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.CountLarge > 1 Then Exit Sub

    If Target.Column = 1 and Target.Value = "Inactive" then
        Application.EnableEvents = False
        Target.Resize(1, 2).Cut _
          Sheets("Inactive(12mths").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
        Application.EnableEvents = True
    End If

End Sub

Sheets("Inactive(12mths")处有错字吗?