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
 

Some videos you may like

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

HalfAce

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

Watch MrExcel Video

Forum statistics

Threads
1,109,445
Messages
5,528,800
Members
409,835
Latest member
Mafu1267

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top