Cancel Button When Importing File

helpexcel

Well-known Member
Joined
Oct 21, 2009
Messages
656
I'm using the following code to import a another file, I thought I fixed the issue of getting a runtime error, but now it doesn't open the selected file and I get the MSG Box.

Dim sPath As String
Dim sFile


Dim ws1 As Worksheet
Set ws1 = Sheet1


sPath = "\\Address\Folder1\Folder2\Folder3" & ws1.Range("A2")
If SetFilePath(sPath) = 0 Then
MsgBox "Error in setting the path - " & sPath
Else
sFile = Application.GetOpenFilename("Excel,*.xls;*.xlsx")
If sFile <> False Then Set WB = Workbooks.Open(sFile) Else
MsgBox "No File Selected"
Exit Sub

End If
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi helpexcel,

Which msgbox is displaying? Is your SetFilePath function returning a 0? Or is sFile not equal to False? Please place your code in code brackets by clicking the # button at the top right of your editor.

Also, it seems to me that you are missing an End If.

Code:
Dim sPath As String
Dim sFile

Dim ws1 As Worksheet
Set ws1 = Sheet1

sPath = "\\Address\Folder1\Folder2\Folder3" & ws1.Range("A2")


If SetFilePath(sPath) = 0 Then
    MsgBox "Error in setting the path - " & sPath
Else
    sFile = Application.GetOpenFilename("Excel,*.xls;*.xlsx")
    If sFile <> False Then
        Set wb = Workbooks.Open(sFile)
    Else
        MsgBox "No File Selected"
        Exit Sub
    [COLOR=#ff0000]End If [/COLOR]'You need this here I think


End If
 
Last edited:
Upvote 0
The second MSG Box shows. Basically I want that box to show and exit sub if the cancel button is hit on the file select dialogue box.


Sorry - I have the 2nd End If, I just cut it off while copy/pasting

Code:
[COLOR=#333333]Dim sPath As String[/COLOR]
[COLOR=#333333]Dim sFile[/COLOR]


[COLOR=#333333]Dim ws1 As Worksheet[/COLOR]
[COLOR=#333333]Set ws1 = Sheet1[/COLOR]


[COLOR=#333333]sPath = "\\Address\Folder1\Folder2\Folder3" & ws1.Range("A2")[/COLOR]
[COLOR=#333333]If SetFilePath(sPath) = 0 Then[/COLOR]
[COLOR=#333333]MsgBox "Error in setting the path - " & sPath[/COLOR]
[COLOR=#333333]Else[/COLOR]
[COLOR=#333333]sFile = Application.GetOpenFilename("Excel,*.xls;*.xlsx")[/COLOR]
[COLOR=#333333]If sFile <> False Then Set WB = Workbooks.Open(sFile) Else[/COLOR]
[COLOR=#333333]MsgBox "No File Selected"[/COLOR]
[COLOR=#333333]Exit Sub[/COLOR]

[COLOR=#333333]End If
[/COLOR]
 
Upvote 0
OK, I reordered the code and now it's working. Not sure why i need to put the "Then" and "Else" on different lines. The cancel part was working, but the import was getting stuck and displaying the message also.

Code:
Dim sPath As String
Dim sFile


Dim ws1 As Worksheet
Set ws1 = Sheet1


sPath = "\\Address\Folder1\Folder2\Folder3\" & ws1.Range("A2")


If SetFilePath(sPath) = 0 Then
    MsgBox "Error in setting the path - " & sPath
Else
    sFile = Application.GetOpenFilename("Excel,*.xls;*.xlsx")
End If




If sFile <> False Then
    Set WB = Workbooks.Open(sFile)
Else
    MsgBox "No File Selected"
        Exit Sub
End If
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

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