Forum

Email Notifications...
 
Notifications
Clear all

Email Notifications when excel file modified

10 Posts
3 Users
0 Reactions
167 Views
(@ninak)
Posts: 9
Active Member
Topic starter
 

Hi Mynda,

 

I have a shared file that multiple users use and I have created a code that highlight in red a cell that was changed and send an email notification.

However, it sends email every time when each cell is modified. Can the email notification be send when the file is saved and closed?

Also is it possible to add into email who changed the file? Please see attached my file let me know if you can help me.

 

Thank you,

Nina

 
Posted : 20/10/2016 3:23 pm
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi Nina,

Using Worksheet_SelectionChange the VBA is executed every time you move to a different cell.  By using Worksheet_Change you can limit the code execution to only when a cell is changed.  And then by testing the interior colour of the cell (as you did) you can check if its one of the cells you care about.  This code is in the Sheet module.

You can use the Workbook_BeforeClose event to send the email before the workbook is closed.  This code is in the ThisWorkbook module.  I've just created a dummy event handler in there for you to put in whatever code you wish.

The user name can be gotten in a variety of ways.  You can use Application.UserName or Environ("username"). You can use Envrion to get the computer name and domain too.

If you are on a network you can also use calls to WScript to get the username, computer name and domain.

I've included code to do all of this in the Worksheet_Change sub.  Without knowing your environment I don't know if this is sufficient for what you want to do. 

There are others ways to get the username but they require a bit more setup (references in the VBA editor, function declarations) so might be over complicating things.  The simple options above should be fine.  The PC environment variables (accessed with Environ) should be set according to the network username/domain/PC Name and should hopefully be sufficient to identify someone.

regards

Phil

 
Posted : 20/10/2016 10:25 pm
(@ninak)
Posts: 9
Active Member
Topic starter
 

Hi Phillip,

 

I see the code but how can I execute it? It does not seem to send the email when I close the file.I have just started working with the vba and really need help with it. Also the Application.UserName or Environ("username")   will work and we are in network I am not sure which option is better I just need a short message that file last was modified by that user that is it.

 

Thank you,

Nina

 
Posted : 21/10/2016 11:36 am
(@ninak)
Posts: 9
Active Member
Topic starter
 

Good morning,

 

Can you please help me with it?

I really need to make it work as soon as possible.

 

Thanks,

Nina

 
Posted : 22/10/2016 10:50 am
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi Nina,

The workbook I attached to my last reply contained the event code for you to use.  You just need to copy the email code that you were already using into the appropriate event sub.

I have been looking at this over the weekend, it's not as simple as it may appear.  If you send the email when the workbook is closed, you need to be able to check if the workbook has been changed.  Excel provides a way to do this by checking if the workbook has been saved since any changes were made. 

But, if someone makes changes and then saves the workbook, when you check if the file needs saving, Excel will tell you it doesn't, so effectively you end up thinking the file hasn't been changed when in fact it has.

So if you use Workbook_BeforeClose to test for changes, you may miss some.

You may be better off using Worksheet_Change and checking if the affected cell is one you care about, and send the email then.  Of course you could end up with multiple emails.

Another approach is to use both Worksheet_Change and Workbook_BeforeClose. When a cell you are concerned with changes, Worksheet_Change records this (in a public variable) but doesn't send an email. When the workbook is closed Workbook_BeforeClose checks the public variable and sends an email if it sees that a change has occurred.

I'll write something to do this and post it here

Regards

Phil

 
Posted : 24/10/2016 6:57 am
(@ninak)
Posts: 9
Active Member
Topic starter
 

I see the code that you sent and when I changed something in that file and then closed it does not send the email.

How can I fix this?

 
Posted : 25/10/2016 9:22 am
(@ninak)
Posts: 9
Active Member
Topic starter
 

Is the code you sent supposed to be working or I need to change something?Please advise.

 

Thank you,

Nina

 
Posted : 26/10/2016 1:59 pm
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi Nina,

Yes the workbook I provided has working event code in it.  You just needed to put what code you want into that event handler/sub.

But, as I said in my last reply, this is actually quite complicated to do correctly.  The initial approach could result in either changes not being reported, or emails being sent when in fact no changes were made.

I had to use a combination of different events and track changes using a hidden sheet.

When you open the workbook, you'll only see one sheet in the tabs at the bottom left of your Excel screen.  But if you open the VBA editor (ALT+F11), you'll see there is another sheet called VeryHiddenSheet.  I've set this to be VeryHidden which is a special property that means not only is the sheet not visible from the Excel front end, it can't be unhidden unless you go into the VBA editor.

This new workbook (attached) will track changes correctly on the red cells you are concerned with and send you an email when the workbook is closed.

Of course, VBA must be enabled for the code to work.

Excel can track changes but it does have limitations, but check it out anyway.

regards

Phil

 
Posted : 27/10/2016 12:31 am
(@ninak)
Posts: 9
Active Member
Topic starter
 

Can you please remove attachments in this chain?

I receive emails form other people who using it!

 
Posted : 11/11/2016 10:44 am
(@catalinb)
Posts: 1937
Member Admin
 

Hi Nina,

I removed your email from each file from this topic, you will not receive those messages.

Cheers,

Catalin

 
Posted : 11/11/2016 3:44 pm
Share: