Browse a file in excel vba

luolovepi

Board Regular
Joined
Jun 9, 2011
Messages
116
Hi everyone,
I have the following code to browse a file in disck. But I get an error message over "If desPathName = False Then". How come?
Code:
Sub browseFile()
    desPathName = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls*), *.xls*", Title:="Please select a file")
    If desPathName = False Then
        MsgBox "Stopping because you did not select a file. Reselect a destination file through the menu"
        Exit Sub
    Else
        Workbooks.Open Filename:=desPathName
        Set des = Workbooks.Open(desPathName, True, False)
        Set desSheet = des.Worksheets(desWorksheetName)
    End If
End Sub

Sincerely,
lolo^^
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try

Rich (BB code):
Sub browseFile()
Dim desPathName As Variant
    desPathName = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls*), *.xls*", Title:="Please select a file")
    If desPathName = False Then
        MsgBox "Stopping because you did not select a file. Reselect a destination file through the menu"
        Exit Sub
    Else
        Workbooks.Open Filename:=desPathName
        Set des = Workbooks.Open(desPathName, True, False)
        Set desSheet = des.Worksheets(desWorksheetName)
    End If
End Sub
 
Upvote 0
Wow!!! Thank you Peter.

I wrongly delcared the desPathName as String...

Thanks!

Best regards,
lolo^_^

Try

Rich (BB code):
Sub browseFile()
Dim desPathName As Variant
    desPathName = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls*), *.xls*", Title:="Please select a file")
    If desPathName = False Then
        MsgBox "Stopping because you did not select a file. Reselect a destination file through the menu"
        Exit Sub
    Else
        Workbooks.Open Filename:=desPathName
        Set des = Workbooks.Open(desPathName, True, False)
        Set desSheet = des.Worksheets(desWorksheetName)
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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