Opening a Workbook from a File Dialog Box

RawlinsCross

Active Member
Joined
Sep 9, 2016
Messages
315
I would have thought that this should be fairly straight forward but I'm running into issues on opening additional workbooks from a userform. So yes, I have a userform with a button that when clicked opens up a number of additional workbooks.

Is this a problem with the string sent to "Workbooks.Open"

Code:
Private Sub LoadSAP_Click()

Dim fd As Office.FileDialog
Dim strFile As String
Dim i As Integer

Set fd = Application.FileDialog(msoFileDialogFilePicker)

With fd
    .Filters.Clear
    .Filters.Add "Excel Files", "*.xlsx?", 1
    .Title = "Choose 2 Files"
    .AllowMultiSelect = True
    .InitialFileName = "C:\Users\Username\Desktop\VBA Projects\PM Monitoring\"
    
    If .Show = True Then
        For i = 1 To .SelectedItems.Count
            strFile = Dir(.SelectedItems(i))
            Workbooks.Open (strFile)
        Next i
    End If
    
End With


End Sub
 

Some videos you may like

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,099
Office Version
  1. 2019
Platform
  1. Windows
Hi,
looking at your code, I suspect your variable strFile excludes the folder path & just contains the file name

try replacing this line

Code:
strFile = Dir(.SelectedItems(i))


with this

Code:
strFile = .SelectedItems(i)


Dave
 

Watch MrExcel Video

Forum statistics

Threads
1,109,273
Messages
5,527,712
Members
409,784
Latest member
AdamPriest

This Week's Hot Topics

Top