Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

On Error Excel 2007 VBA: Getting it in the right place

Posted on 2012-12-26
5
Medium Priority
?
159 Views
Last Modified: 2012-12-27
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 workbook

If Range("checksave").Value = 3 Then
MsgBox ("Created")
ActiveWorkbook.SaveAs Filename:=Range("path").Text & Range("filename").Text
On Error GoTo Err

::::My code here

Else: MsgBox ("You must enter a MID and Customer ID and Business Name before saving.")
Exit Sub
End If
Err:
MsgBox ("You must enter a MID and Customer ID and Business Name before saving.")
Worksheets("Dashboard").Shapes("Rounded Rectangle 50").Delete

End Sub

Open in new window

0
Comment
Question by:Dreamboat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
tdlewis earned 1600 total points
ID: 38722202
Swap lines 12 and 13 of your code so that you have:
Else: MsgBox ("You must enter a MID and Customer ID and Business Name before saving.")
End If
Exit Sub

Open in new window

0
 
LVL 2

Assisted Solution

by:hanklm
hanklm earned 400 total points
ID: 38722530
The On Error Goto command affects all commands in the Sub that follow it, so if you wanted to detect an error during file save, you would want the On Error statement  before the SaveAs call and any other statements you expect might fail.

If code logic detects an error, jump to error handling code.  In your example this avoids duplicating the MsgBox code, but it is worth noting that the SaveAs command can encounter a wide range of errors, so you might also want to check the value of ErrNo for situations like path-not-found or access-denied, etc.

Sub SaveBook()
'Saves the workbook

On Error GoTo Err
If Range("checksave").Value = 3 Then
MsgBox ("Created")
ActiveWorkbook.SaveAs Filename:=Range("path").Text & Range("filename").Text

::::My code here

Else
goto Err
End If

Exit Sub
Err:
MsgBox ("You must enter a MID and Customer ID and Business Name before saving.")
Worksheets("Dashboard").Shapes("Rounded Rectangle 50").Delete

End Sub

Open in new window

0
 
LVL 22

Author Comment

by:Dreamboat
ID: 38722588
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!
0
 
LVL 22

Author Comment

by:Dreamboat
ID: 38723502
Here's what I'm using, which appears to work. :)

Sub SaveBook()
'Saves the workbook

If Range("checksave").Value = 3 Then
MsgBox ("Created")
ActiveWorkbook.SaveAs Filename:=Range("path").Text & Range("filename").Text
On Error GoTo Err

:::My code here

Else: MsgBox ("You must enter a MID and Customer ID and Business Name before saving.")
Worksheets("Dashboard").Shapes("Rounded Rectangle 50").Delete
End If
Exit Sub

End Sub

Open in new window

0
 
LVL 2

Expert Comment

by:hanklm
ID: 38724867
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.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question