Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

not running code certain

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Not running code for certain users

Not running code for certain users

ResolvedVersion 2010

Matthew has attended:
Excel VBA Intro Intermediate course

Not running code for certain users

I have created a tracker sheet for other departments to use that email myself & my manager when a change is made.

This is the code below that I am using

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)

Dim answer As String

answer = MsgBox("Do you wish to save changes?", vbYesNo + vbQuestion, "Save")

If answer = vbNo Then Cancel = True
If answer = vbYes Then
'open outlook
Set OutlookApp = CreateObject("Outlook.Application")
Set OlObjects = OutlookApp.GetNamespace("MAPI")
Set newmsg = OutlookApp.CreateItem(olMailItem)
'add recipients
newmsg.Recipients.Add ("Email Address")
'add subject
newmsg.Subject = "Appraisal Tracker"
'Add body
newmsg.body = "Changes have been made to the appraisal tracker"
newmsg.Display 'display
newmsg.Send 'send message


End If

End Sub

Is there way to stop the code from running when I make a change to the document?

Thank you in advance

Matt


RE: Not running code for certain users

Hi Matthew,

Thank you for the forum question.

You can do it by using one more IF decision code. The Application.UserName function will test the user name'



Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)



If Application.UserName <> "Jens Bonde" And Application.UserName <> "Matthew" Then

answer = MsgBox("Do you wish to save changes?", vbYesNo + vbQuestion, "Save")

If answer = vbNo Then
Cancel = True
end if
If answer = vbYes Then
'open outlook
Set OutlookApp = CreateObject("Outlook.Application")
Set OlObjects = OutlookApp.GetNamespace("MAPI")
Set newmsg = OutlookApp.CreateItem(olMailItem)
'add recipients
newmsg.Recipients.Add ("Email Address")
'add subject
newmsg.Subject = "Appraisal Tracker"
'Add body
newmsg.body = "Changes have been made to the appraisal tracker"
newmsg.Display 'display
newmsg.Send 'send message


End If

End If





End Sub








I hope this make sense.





Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

Tue 14 Feb 2017: Automatically marked as resolved.

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

Counting Non Number Cells (Text)

If you try to use the COUNT FUNCTION =COUNT(Cell range)with a range of cells with numbers and or containing text fields you wil find that that the text cells will be excluded from the the count. If you want to include them try the the COUNTA FUNCTION =COUNTA(Cell range). This counts both text and number cell values.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.1 secs.