Programing a code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am new to vba and have a pretty simple, i think, question.

How do you program a message box to pop up in a workbook?

Example: If I have a template that other employees are using, i would like a
error box to pop up if they do not enter their name in cell C8, or if they
have overtime they must enter in notes in a certain column, or if their total
hours do not equal 80 upon saving the file.

Any suggestions?
 
You probably do not want to keep people from being able to save if the sheet
is not filled out correctly but a friendly reminder is in order. You can use
code like this. Right click on the XL icon in th eupper left corner of you
Excel and select View Code and paste the following...

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Sheets("Sheet1").Range("C8").Value = "" Then _
MsgBox "Please enter your name.", vbInformation, "Name Error"
If Sheets("Sheet1").Range("D50").Value <> 80 Then _
MsgBox "Please ensure your hours total 80.", vbInformation, "Hours Error"
End Sub

As for requiring notes to be enetered I would be inclined to just use
conditional formatting to highlight any areas where comments need to be
included if overtime is entered.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top