VBA Excel获取更新的单元格的值

好的,所以我一直在努力解决这个问题。 我对VBA相当陌生,所以请耐心等待。

我需要的程序是获取已更新的工作簿中的单元格值,并将数据推送到电子邮件。 假设我想看的范围是A4:A100。 用户将销售订单编号input到A10和A11中。 我需要我的程序从这些单元格中获取值,并在保存工作簿时将其插入到电子邮件中。 在这个问题的最后,我已经有了这个问题的代码,当工作簿被保存时,这些代码会通过电子邮件发送人员列表。 基本上我非常希望这个电子邮件包含inputlogging的ID(列A)。 感谢您的帮助。

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _ Cancel As Boolean) Dim answer As String answer = MsgBox("Would you like to save and email a notification?", vbYesNo, "Save and Email") If answer = vbNo Then Cancel = True If answer = vbYes Then 'open outlook type stuff Set OutlookApp = CreateObject("Outlook.Application") Set OlObjects = OutlookApp.GetNamespace("MAPI") Set newmsg = OutlookApp.CreateItem(olMailItem) 'add recipients newmsg.Recipients.Add ("will.smead@cablevey.com") newmsg.Recipients.Add ("will.smead@cablevey.com") 'add subject newmsg.Subject = "Update on CREDIT HOLD list" 'add body newmsg.Body = "Please check the Credit Hold excel file on the S drive for updates." newmsg.Display 'display newmsg.Send 'send message 'give conformation of sent message MsgBox "File saved and messages sent.", , "Saved and Sent" End If 'save the document 'Me.Worksheets.Save End Sub 

您需要在工作表中更改活动,

 Sub worksheet_change(ByVal target As Range) If Not Intersect(target, Range("A4:A100")) Is Nothing Then Dim ws As Worksheet Set ws = Sheets("changes") Dim lastrow As Integer lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row ws.Cells(lastrow + 1, 1) = target.Address ws.Cells(lastrow + 1, 2) = target End If End Sub 

你需要创build一个名为changes的工作表,并把第一行col A和B放在“address”和“new value”中 –

在这里输入图像说明

你可以看到我有一个拼写错误,必须纠正它。 你应该在上面的macros中sorting这些东西。