On Error Excel 2007 VBA: Getting it in the right place
I want the Msgbox to show ONLY if file can't save or doesn't have values in the 3 cells. I never seem to be able to figure out where to put it so it does NOT pop up always.
Sub SaveBook()'Saves the workbookIf Range("checksave").Value = 3 ThenMsgBox ("Created")ActiveWorkbook.SaveAs Filename:=Range("path").Text & Range("filename").TextOn Error GoTo Err::::My code hereElse: MsgBox ("You must enter a MID and Customer ID and Business Name before saving.")Exit SubEnd IfErr:MsgBox ("You must enter a MID and Customer ID and Business Name before saving.")Worksheets("Dashboard").Shapes("Rounded Rectangle 50").DeleteEnd Sub
so you might also want to check the value of ErrNo for situations like path-not-found or access-denied, etc.
Nope. They won't get that. If they do, it'll be once and never again.
Thanks, guys! I'll check tomorrow at work. Happy Holidays!
Anne Troy
ASKER
Here's what I'm using, which appears to work. :)
Sub SaveBook()'Saves the workbookIf Range("checksave").Value = 3 ThenMsgBox ("Created")ActiveWorkbook.SaveAs Filename:=Range("path").Text & Range("filename").TextOn Error GoTo Err:::My code hereElse: MsgBox ("You must enter a MID and Customer ID and Business Name before saving.")Worksheets("Dashboard").Shapes("Rounded Rectangle 50").DeleteEnd IfExit SubEnd Sub
It looks like you removed the label: Err. This is not good. I'm not sure what the behavior will be when an error is encountered, but I doubt it will be what you want.
I suspect if you compiled the VBA project. It would tell you that the label could not be found. I highly recommend using the compile feature (Debug / Compile VBAProject). It helps discover errors (like this one) you might otherwise miss.
Thanks, guys! I'll check tomorrow at work. Happy Holidays!