查找列中所有更改的单元格,并将其放入一个variables中

我在Sheet2有一个与macros(“新项目”)更改的列。 每次运行该macros时,C列中的单元格都会更改其内容。

我想获得所有添加了“新项目”macros的string(来自列C中的单元格),并将它们全部放在一个variables中。 我需要该variables(包含更改的单元格内容)通过电子邮件发送。

我想我必须使用下面的函数,但我不知道如何去做我所需要的。 下面的代码不起作用。

  Private Sub Worksheet_Change(ByVal Target As Range) 'if column C changes If Target.Address = Sheet(2).Range("C15:C"&lastRow) Then dim var as string 'put the contents of cells changed in the variable "var" var=range("?").Value End If End Sub 

我会build议一个不同的方法。 原因很简单。 每次更改单元格时, Worksheet_Change都会触发,这会减慢代码的速度。

当该macros启动时(更改Sheet2Sheet2那个),将Sheet2中的Sheet2复制到新临时表中,并在macros结束之前将ColCSheet2再次复制到临时表中,然后简单地比较两者列检查什么改变。

例如( UNTESTED

 Sub Sample() Dim wsNew As Worksheet, wsI As Worksheet Dim lRow As Long Dim sNewvalues As String Set wsI = ThisWorkbook.Sheets("Sheet2") Set wsNew = ThisWorkbook.Sheets.Add '~~> Copy the column 3 into column 1 of the '~~> new sheet before macro makes changes wsI.Columns(3).Copy wsNew.Columns(1) ' '~~> Rest of the macro ' '~~> Copy the column 3 into column 2 of the '~~> new sheet after macro makes changes wsI.Columns(3).Copy wsNew.Columns(2) With wsNew '~~> Get last row of thenew sheet If Application.WorksheetFunction.CountA(.Cells) <> 0 Then lRow = .Cells.Find(What:="*", _ After:=.Range("A1"), _ Lookat:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Row Else lRow = 1 End If '~~> Add a formula in the 3rd column to see if there is any difference .Range("C1:C" & lRow).Formula = "=A1=B1" '~~> Store the diffrence in a variable For i = 1 To lRow If .Range("C" & i).Value = "True" Then If sNewvalues = "" Then sNewvalues = .Range("B" & i).Value Else sNewvalues = sNewvalues & vbNewLine & .Range("B" & i).Value End If End If Next i '~~> Display the values MsgBox sNewvalues End With '~~> Delete the temp sheet Application.DisplayAlerts = False wsNew.Delete Application.DisplayAlerts = True End Sub 

我看到的直接问题是:

  • 如上所述,如果第14行下方的列C中的单元格在“新build项目”macros之外被更改,那么预期会发生什么?
  • 许多macros(可能是“新build项目”)在其操作过程中使用Application.EnableEvents = False,这有效地取消了WorkSheet_Change事件。 需要确认,这不是'新项目'macros的情况。

重写原始回复。 这包括一个子例程,以通知的forms通过电子邮件发送更改。

 Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Target, Range("C15:C" & Cells(Rows.Count, 3).End(xlUp).Row)) Is Nothing Then On Error GoTo FallThrough Application.EnableEvents = False Dim c As Range, cs As String For Each c In Intersect(Target, Range("C15:C" & Cells(Rows.Count, 3).End(xlUp).Row)) cs = cs & c.Address(0, 0) & ": " & c.Value & " - " & Format(Now, "dd-mmm-yyyy hh:mm") & Chr(10) Next c cs = Left(cs, Len(cs) - 1) Call mcr_Email_Notification("New Item Notification", cs) End If FallThrough: Application.EnableEvents = True End Sub Sub mcr_Email_Notification(sSBJ As String, sBDY As String) Dim objOL As Outlook.Application, objOLMSG As Outlook.MailItem Set objOL = CreateObject("Outlook.Application") Set objOLMSG = objOL.CreateItem(olMailItem) With objOLMSG .To = "some.recipient@null.com" 'change this .Subject = sSBJ .HTMLBody = "<html><body>" & Replace(sBDY, Chr(10), "<br/>") & "</body></html>" .send End With Set objOLMSG = Nothing Set objOL = Nothing End Sub 

电子邮件通知例程需要添加工具,参考,Microsoft Outlook 15.0对象库 (或等效)。 我已经testing并收到了电子邮件中第14行C列的更改/添加的详细信息。

此代码属于工作表代码表,而不是模块代码表。 用鼠标右键单击工作表名称选项卡并select查看代码 。 当VBE打开时,将其粘贴到标题为Book1 – Sheet2(Code)的窗格中。