Private Sub Worksheet_SelectionChange / Worksheet_Change

时间:2017-09-27 16:14:32

标签: excel vba excel-vba worksheet-function

美好的一天,我需要在一个工作簿中创建2个私有宏 - 一个在单击后存储单元格内容,另一个用于存储单元格的新值,并在单元格中发送和发送旧文本正文。单元格中的新文本。

说实话,我不确定这是否是正确的方法(或者甚至可能),但我不经常使用私有宏,所以我会感谢任何帮助。非常感谢!

这就是我到目前为止所得到的:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
OldCellValue = ActiveCell.text
old_value = OldCellValue
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)

Dim Area As Range
Dim OutlApp As Object
Dim IsCreated As Boolean
Dim cell As String
Dim old_value As String
Dim new_value As String

Set Area = Range("A1:E20")

If Target.Cells.Count > 1 Then Exit Sub

If Not Intersect(Target, Area) Is Nothing Then

cell = ActiveCell.Address
new_value = ActiveCell.text

On Error Resume Next
 Set OutlApp = GetObject(, "Outlook.Application")
 If Err Then
   Set OutlApp = CreateObject("Outlook.Application")
   IsCreated = True
 End If
 OutlApp.Visible = True
 On Error GoTo 0

 With OutlApp.CreateItem(0)

   .Subject = "Change in table"
   .to = "someones email"
   .HTMLBody = "Change in cell " & "<B>" & cell & "</B><br>" _
                & "Old value: " & old_value & "New value: " & new_value

   On Error Resume Next
   .Send
   Application.Visible = True
   On Error GoTo 0

 End With

 If IsCreated Then OutlApp.Quit

 Set OutlApp = Nothing

End With

End If

End Sub

2 个答案:

答案 0 :(得分:0)

不重写所有代码,但实质上,您必须执行此操作以在选择单元格时存储值,然后在更改单元格后存储该值。您不需要newcellvalue变量,因为Target会捕获该变量。

Dim OldCellValue

Private Sub Worksheet_Change(ByVal Target As Range)

Dim newcellvalue

newcellvalue = Target.Value

MsgBox "Old " & OldCellValue & ", New " & newcellvalue

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

OldCellValue = Target.Value

End Sub

答案 1 :(得分:0)

我认为你走在正确的轨道上。我将使用全局变量来跟踪当前/旧值,以便您可以在Worksheet_Change事件中进行比较。

这样的事情:

Private old_value As String

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Target.Cells.Count > 1 Then Exit Sub

    old_value = Target.Text

    'Debug to check the old_value
    'Debug.Print "old_value = " + old_value
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim Area As Range
    Dim OutlApp As Object
    Dim IsCreated As Boolean
    Dim cell As String
    Dim new_value As String

    Set Area = Range("A1:E20")

    If Target.Cells.Count > 1 Then Exit Sub

    If Not Intersect(Target, Area) Is Nothing Then
        new_value = Target.Text

        'Debug to compare values
        'Debug.Print "new_value = " + new_value
        'Debug.Print "old_value = " + old_value

        If new_value <> old_value Then
            'Debug to compare
            'Debug.Print "new_value and old_value are different"
        End If
    End If
End Sub

请记住,每次在单元格之间导航时,都会触发Worksheet_SelectionChange事件。因此,如果更改单元格的值并按Enter键,则old_value的值将更改,因为您正在重置Worksheet_SelectionChange事件中的值。您需要执行比较并在选择更改之前发送电子邮件。

此外,您可能希望将.Value用于单元格而不是.Text。请参阅此帖子了解差异:What is the difference between .text, .value, and .value2?