Basically I need to open two files in the same workbook so I can preform some calculations on the values.
I can open a single file with the code below:
Sub sbVBA_To_Open_Workbook_FileDialog_xls_C() Dim strFileToOpen As String strFileToOpen = Application.GetOpenFilename _ (Title:="Please select an Excel file to open", _ FileFilter:="Excel Files *.xls (*.xls),") If strFileToOpen = False Then MsgBox "No file selected.", vbExclamation, "Sorry!" Exit Sub End If Workbooks.Open strFileToOpen End Sub
I did find some code online that allows a user to select multiple files but mine are stored in different locations so it would be easier to have two prompt boxes.
It would help if I could load them into separate sheets e.g.
Many thanks for your help!
This will prompt for two
*.XLS* filenames and open them both
Option Explicit Sub sbVBA_To_Open_Workbook_FileDialog_xls_C() Dim strFile1 As String, strFile2 As String strFile1 = Application.GetOpenFilename("Excel Files *.xls* (*.xls*),", , "Choose Excel file #1") If strFile1 = "" Then GoTo userCancel strFile2 = Application.GetOpenFilename("Excel Files *.xls* (*.xls*),", , "Choose Excel file #2") If strFile2 = "" Then GoTo userCancel Workbooks.Open strFile1 Workbooks.Open strFile2 Exit Sub userCancel: MsgBox "User cancelled.", vbExclamation, "Sorry!" End Sub
It won’t combine them into a worksheet but nore details would be needed about that. ie., how are they combined? what if there’s more than 1 worksheet in the source workbook?
…but these 2 can remain side by side
The terms, “Workbook”, and “File”, are synonymous. When a file is opened it becomes a workbook in the workbooks collection. If you open several workbooks you can display their names with the following code:
Private Sub ShowWorkbookNames() Dim wb As Workbook For Each wb In Application.Workbooks Debug.Print wb.Name Next wb End Sub
So if you have two different files open in Excel (Say, “Invoices.xlsm”, and, “Download.csv”) then running the above code will deliver the following output to the Immediate Window, which is viewable via ctrl+G:
You won’t want to construct a third workbook containing one sheet from each of the first two workbooks, but that doesn’t matter as you’ll be able to access both worksheets in a similar way as you access two worksheets in the same workbook.
Private Sub IdentifyWorksheetsInSeperateWorkbooks() 'Workbook 1 Dim wb1 As Workbook Dim ws1 As Worksheet Set wb1 = Application.Workbooks("Invoices.xlsm") Set ws1 = wb1.Sheets("Accounts") Debug.Print ws1.Name 'Workbook 2 Dim wb2 As Workbook Dim ws2 As Worksheet Set wb2 = Application.Workbooks("Download.csv") Set ws2 = wb2.Sheets(1) Debug.Print ws2.Name End Sub
Running the above code will create the following output in the immediate window.
And then you can do all the things that you normally do with the worksheets, ws1 and ws2, and remain completely oblivious to the fact that they are, in fact, in different workbooks.
IF you need more than 2 files to open, this will loop through the open dialog box, until you choose cancel or this X button on the dialog box.
You will need more code to do anything with the files after they are open.
Sub sbVBA_To_Open_Workbook_FileDialog_xls_C() Dim strFileToOpen As String Do strFileToOpen = Application.GetOpenFilename _ (Title:="Please select an Excel file to open", _ FileFilter:="Excel Files *.xls* (*.xls*),") If strFileToOpen = "False" Then MsgBox "No file selected.", vbExclamation, "Sorry!" Exit Sub End If Workbooks.Open strFileToOpen Loop End Sub