错误13与下拉列表审核日志

时间:2016-07-28 10:14:38

标签: excel vba excel-vba

我正在尝试为报告执行审核日志。报告是状态部分之一,可以通过下拉列表进行选择。这是我的代码。

Option Explicit
Dim PreviousValue As Variant  



Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("D2")) Is Nothing Then
        Range("E" & Target.Row & ":F" & Target.Row).ClearContents
        If Range("D2").Value = "I/C" Then
        Range("E" & Target.Row).Locked = True
        Range("E" & Target.Row & ":F" & Target.Row).ClearContents
        Else
        Range("E" & Target.Row).Locked = False
        End If

    End If

    If Not Intersect(Target, Range("D3")) Is Nothing Then
         Range("E" & Target.Row & ":F" & Target.Row).ClearContents
    End If
    If Not Intersect(Target, Range("D4")) Is Nothing Then
        Range("E" & Target.Row & ":F" & Target.Row).ClearContents
    End If
    If Not Intersect(Target, Range("D5")) Is Nothing Then
         Range("E" & Target.Row & ":F" & Target.Row).ClearContents
    End If

    Sheets("GC-01 History Log").Cells(1, 1).Value = "Date"
    Sheets("GC-01 History Log").Cells(1, 2).Value = "Equipment"
    Sheets("GC-01 History Log").Cells(1, 3).Value = "Old Status"
    Sheets("GC-01 History Log").Cells(1, 4).Value = "New Status"
    Sheets("GC-01 History Log").Cells(1, 5).Value = "Old Reason"
    Sheets("GC-01 History Log").Cells(1, 6).Value = "New Reason"
    Sheets("GC-01 History Log").Cells(1, 7).Value = "Old Action"
    Sheets("GC-01 History Log").Cells(1, 8).Value = "New Action"

    If Target.Value <> PreviousValue Then
   Sheets("GC-01 History Log").Cells(65000, 1).End(xlUp).Offset(1, 0).Value = Now
   Sheets("GC-01 History Log").Cells(65000, 2).End(xlUp).Offset(1, 0).Value = Range(Target.Address).Offset(0, -1)
   Sheets("GC-01 History Log").Cells(65000, 3).End(xlUp).Offset(1, 0).Value = Target.Cells(Target.Row, 1)
   Sheets("GC-01 History Log").Cells(65000, 4).End(xlUp).Offset(1, 0).Value = Target.Value

End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
PreviousValue = Target.Value

End Sub

问题在于,当我尝试通过下拉菜单更改状态时。错误13被抛出。我不知道如何处理这个问题。

编辑: - 在调试时,它显示错误是“Target.Value&lt;&gt; PreviousValue Then”

请帮助

1 个答案:

答案 0 :(得分:0)

两件事:

首先:这个

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    PreviousValue = Target.Value
End Sub

仅在您实际更改您的选择时才有效。因此,这可能会导致PreviousValue为空。

第二个也是更重要的:

您希望将PreviousValue声明为Public,否则您无法在两个子例程中使用它。

Option Explicit
Public PreviousValue As Variant
...

是你想要的。