Joy:
Gord's solution is perfect. I'm just including this code because I do this
function so often that I created a callable subroutine that I use to open
files.
Option Explicit
Option Base 1
' ************************************************
' Variables For File Open Dialogue Box
' ************************************************
Public strDialogueFileTitle As String
Public strFilt As String
Public intFilterIndex As Integer
Public strCancel As String
Public strWorkbookFullPathAndName As String
Public strWorkbookOnlyName As String
Public Sub ExampleOfCallingOpenAFile()
' ****************************************************************************
' Present the Open File Dialogue To The User
' ****************************************************************************
strFilt = "Excel Files (*.xls),*.xls," & _
"CSV Files (*.csv),*.csv,"
intFilterIndex = 1
strDialogueFileTitle = "Select Your Input File of Choice"
Call OpenFileDialogue
If strCancel = "Y" Then
MsgBox ("An Open Error Occurred Importing Your File Selection")
Exit Sub
End If
' ****************************************************************************
' Confirmation
' ****************************************************************************
MsgBox ("You Opened Workbook " & strWorkbookOnlyName)
MsgBox ("Your Current Worksheet is " & ActiveSheet.Name)
End Sub
Sub OpenFileDialogue()
' ************************************************
' Display a File Open Dialogue Box For The User
' ************************************************
strCancel = "N"
strWorkbookFullPathAndName = Application.GetOpenFilename _
(FileFilter:=strFilt, _
FilterIndex:=intFilterIndex, _
Title:=strDialogueFileTitle)
' Exit If No Filename Selected
If strWorkbookFullPathAndName = "" Then
MsgBox ("No Filename Selected")
strCancel = "Y"
Exit Sub
ElseIf strWorkbookFullPathAndName = "False" Then
MsgBox ("You Clicked The Cancel Button")
strCancel = "Y"
Exit Sub
End If
' ************************************************
' Open The Workbook Selected
' ************************************************
Workbooks.Open strWorkbookFullPathAndName
' ************************************************
' Save the Workbook Name Without the Path
' ************************************************
strWorkbookOnlyName = ActiveWorkbook.Name
End Sub