VBA Prompt User to Select Excel File to Import

BigNate

Board Regular
Joined
Dec 17, 2014
Messages
242
Hello Everyone,

I'm struggling with this one. I have a button called "BtnImportExcel". I want to click this button and have a box appear so that the user can select the Excel file the they want to import, much like an email attachment.

Can someone please tell me how to do this or point me in the right direction? Thanks!:)
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Add this code to the button click. NOTE, BE SURE TO ADD : Microsoft Office 11.0 Object Library
to the project REFERENCES.

usage:
Rich (BB code):
sub btnImportExcel_click()
vFile = UserPick1File("c:\folder\")
if vFile <> "" then
    docmd.transferspreadsheet .....
endif
end sub






Public Function UserPickFile(byval pvPath)
Dim strTable As String
Dim strFilePath As String
Dim sDialog As String, sDecr  As String, sExt As String
dim fd as filedialog


set fd = Application.FileDialog(msoFileDialogFilePicker)  '<---- 'MUST ADD REFERENCE : Microsoft Office 11.0 Object Library
With fd
    .AllowMultiSelect = False
    .Title = "Locate a file to Import"
    .ButtonName = "Import"
    .Filters.Clear
    .Filters.Add "All Files", "*.*"
    .Filters.Add "Excel Files", "*.xls*"


    .InitialFileName = pvPath
    .InitialView = msoFileDialogViewList    'msoFileDialogViewThumbnail
    ''.AllowMultiSelect = True
     
        If .show = 0 Then
           'There is a problem
           Exit Function
        End If
    
    'Save the first file selected
    UserPickFile = Trim(.SelectedItems(1))
End With
set fd = nothing
End Function
 
Upvote 0
Hey thanks a lot. One more question for you if you don't mind--what do I do with the line docmd.transferspreadsheet? How do I know what to put here, if it is the file that changes from one click to the next?
 
Upvote 0
that is the import command.
to import the file chosen into the table...file out the items to fit your db.
 
Upvote 0

Forum statistics

Threads
1,214,661
Messages
6,120,792
Members
448,994
Latest member
rohitsomani

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