senators40
Technical User
I have an button on my userform that allows the user to open a file, however if they select a file that is already open, I get a message saying that the file is already open and reopening it will cause any changes you have made to be discarded. Do you still want to reopen the file.
If you say no you will get the following error message
Runtime error '1004:'
Method 'Open' of objects 'Workbooks' failed
The line where it stalls is the "Workbooks.Open fn" - I would like to use a If then statement which would state that if the file selected is open then the "Workbooks.Open fn" does not proceed. However if there are better ways of doing it please let me know
Please note I am also using a listbox to identify the open workbooks (listbox1) just in case that makes it easier.
Below is the code that I am using.
Private Sub CommandButton1_Click()
Dim fn As Variant
fn = Application.GetOpenFilename("Excel-files,*.xls", _
1, "Select One File To Open", , False)
If TypeName(fn) = "Boolean" Then Exit Sub
' the user didn't select a file
Debug.Print "Selected file: " & fn
Workbooks.Open fn
Any help would be appreciated
Thanks,
Jeff
If you say no you will get the following error message
Runtime error '1004:'
Method 'Open' of objects 'Workbooks' failed
The line where it stalls is the "Workbooks.Open fn" - I would like to use a If then statement which would state that if the file selected is open then the "Workbooks.Open fn" does not proceed. However if there are better ways of doing it please let me know
Please note I am also using a listbox to identify the open workbooks (listbox1) just in case that makes it easier.
Below is the code that I am using.
Private Sub CommandButton1_Click()
Dim fn As Variant
fn = Application.GetOpenFilename("Excel-files,*.xls", _
1, "Select One File To Open", , False)
If TypeName(fn) = "Boolean" Then Exit Sub
' the user didn't select a file
Debug.Print "Selected file: " & fn
Workbooks.Open fn
Any help would be appreciated
Thanks,
Jeff