vba: if error show msgbox

cmhoz

Active Member
Joined
Aug 20, 2006
Messages
268
I have a subroutine that opens a file based on some input from the user.... generally the file it looks for has a standard naming format used. However sometimes the person responsible for this will use some other name, thus screwing up my macro.

Rather than excel showing the generic error box, confusing the user completely, I want it to show MY msgbox telling the user to go in and find the file manually...

I'm sure I've done this before, but I'm drawing a blank on how I managed it. Any help out there??


Sub openphiac()

Dim strfolder as string
Dim strphiacfile as string

strfolder = Range("folder")
strphiacfile = Range("phiacfile")

Workbooks.Open Filename:="O:\Phiac Data\PhiacTables\" & strfolder & "\" & strphiacfile & ".xls"

End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi cmhoz
Something like this ought to do it.
Code:
Sub openphiac()

Dim strfolder As String
Dim strphiacfile As String

strfolder = Range("folder")
strphiacfile = Range("phiacfile")

On Error GoTo ErrMsg
Workbooks.Open Filename:="O:\Phiac Data\PhiacTables\" & strfolder & "\" & strphiacfile & ".xls"
Exit Sub

ErrMsg:
MsgBox ("Type in your message here."), , "MESSAGE TITLE"
End Sub
 
Upvote 0
I can't test it, but maybe this:

Code:
Sub openphiac()
On Error GoTo Errormask
Dim strfolder As String
Dim strphiacfile As String

strfolder = Range("folder")
strphiacfile = Range("phiacfile")

Workbooks.Open Filename:="O:\Phiac Data\PhiacTables\" & strfolder & "\" & strphiacfile & ".xls"
Errormask:
MsgBox "Text Here!"
End Sub
 
Upvote 0
works like a charm... except that the errmsg is coming up always... how do I tell it not to show that unless there IS an error?

can I use "Is error" or something similar in an If or Select Case statement??
 
Upvote 0
works like a charm... except that the errmsg is coming up always... how do I tell it not to show that unless there IS an error?
Which code are you using?
With the line Exit Sub before the line ErrMsg: it should quit the sub before
showing the message if there is no error.
 
Upvote 0

Forum statistics

Threads
1,213,490
Messages
6,113,957
Members
448,535
Latest member
alrossman

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top