Excel单元格值的更改会在另一个单元格中插入“S”

时间:2012-03-14 13:33:40

标签: excel vba excel-vba

我已粘贴工作表1。请注意列。要监控的是标题描述和健康状况。一旦在列下更改发生更改,就会插入一个S. E.g Spectacle Frame更改为Spec Frame,我们在Change下插入S.更改列原始为空。在Healthman的价格变化下,在“更改”列下插入了P.当要删除产品,即删除行时,在更改下插入X.当插入一行时,因为我正在按单元格与隐藏的单元格进行比较,这就是我更新隐藏工作表的原因。我尝试了之前的代码,但它没有工作,并考虑粘贴工作表进行审核         图例:P =价格变动
        N = NEW X = DELETED S = DESCRIPTION
        代码更改说明HEALTHMAN

    40501       Spectacle Frame             
    40609       Sunglasses              
    40525   S   Ready Made Optical              
    41008       Spectacle Case              
    43025   P   Transfer - Zyl or Metal  Frame  198.00          
    43126   P   Transfer - Nylon Supra Frame    314.00          
    43136   P   Transfer - Rimless Frame    359.00      

2 个答案:

答案 0 :(得分:5)

使用Worksheet_Change时,您必须确保两件事。

1)必须进行适当的错误处理

2)关闭.EnableEvents以避免无限循环。

<强>的假设

1)您想捕获单元格A1

中的更改

2)您想在A2

中显示“S”

因此,请在下面的代码中进行更改。

<强> CODE

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Whoa

    If Not Intersect(Target, Range("A1")) Is Nothing Then
        Application.EnableEvents = False
        Range("A2").Value = "S"
    End If

LetsContinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume LetsContinue
End Sub

<强>后续

这是你在尝试的吗?

这可以在“ThisWorkbook”代码区域中找到。

Private Sub Workbook_Open()
   Dim hiddenSheet As Worksheet

   Set hiddenSheet = Me.Worksheets.Add
   hiddenSheet.Visible = xlSheetVeryHidden

   On Error Resume Next
   Application.DisplayAlerts = False
   Sheets("HiddenSheet").Delete
   Application.DisplayAlerts = True
   On Error GoTo 0

   hiddenSheet.Name = "HiddenSheet"

   Sheet1.Range("A1:D15").Copy hiddenSheet.Range("A1:D15")
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
   On Error Resume Next
   Application.DisplayAlerts = False
   Sheets("HiddenSheet").Delete
   Application.DisplayAlerts = True
   On Error GoTo 0
End Sub

这将在相关的表格区域

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Whoa

    If Not Intersect(Target, Range("A1:D15")) Is Nothing Then
        Dim old_value

        Application.EnableEvents = False

        old_value = Sheets("HiddenSheet").Range(Target.Address).Value

        If Target.Value <> old_value Then
            '~~> Change 1 to whatever offset that you want.
            Target.Offset(0, 1).Value = "S"
        End If
    End If

LetsContinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume LetsContinue
End Sub

HTH

西特

答案 1 :(得分:0)

您可以使用Worksheet_Change事件。例如,请参阅thisthat

要添加到Workseet的示例代码 - 当您更改另一个单元格时,A3将自动更改,如果更改的单元格为A1,则具有特定行为:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = Range("A1").Address Then
        Range("A3") = "You changed A1"
    ElseIf Target.Address <> Range("A3").Address Then 'to prevent a change of A3 to trigger an update
        Range("A3") = "You changed another cell"
    End If
End Sub