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

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

HalfAce

MrExcel MVP
Joined
Apr 6, 2003
Messages
9,454
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
 

Macropheliac

Board Regular
Joined
Aug 26, 2005
Messages
165
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
 

cmhoz

Active Member
Joined
Aug 20, 2006
Messages
268
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??
 

HalfAce

MrExcel MVP
Joined
Apr 6, 2003
Messages
9,454
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.
 

cmhoz

Active Member
Joined
Aug 20, 2006
Messages
268
ah ha. i'm just blind!

added the Exit Sub and all is well.

Thanks!!
 

Forum statistics

Threads
1,141,138
Messages
5,704,497
Members
421,353
Latest member
jekoxien15

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
Top