Switch between two user selected workbooks with varying names

Human_doing

Board Regular
Joined
Feb 16, 2011
Messages
137
Hi all,

I am using this code to ask the user to open up two workbooks from which the macro will combine some data. The workbooks will always be in the same format but the names will differ.

Code:
Private Sub CommandButton1_Click()
'Ask user to select Excel file
NewFN = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Select the report file")
NewFN2 = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Select the list file")
If NewFN = False Then
    ' They pressed Cancel
    MsgBox "No file selected."
    Exit Sub
Else
    Workbooks.Open Filename:=NewFN
End If
If NewFN2 = False Then
    ' They pressed Cancel
    MsgBox "No file selected."
    Exit Sub
Else
    Workbooks.Open Filename:=NewFN2
End If
 
NewFN.Sheets(1).Activate
 
End Sub

I was thinking this code assigns the first selection the file name NewFn and therefore I could use line of code NewFN.Sheets(1).Activate to navigate to that workbook but I receive error 'object required', could anyone please assist with allowing the code to assign 'names' to each workbook so I can reference them in other sections of the code?

Many thanks
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try:

Rich (BB code):
Private Sub CommandButton1_Click()
Dim wb1 As Workbook, wb2 As Workbook
'Ask user to select Excel file
NewFN = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Select the report file")
NewFN2 = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Select the list file")
If NewFN = False Then
    ' They pressed Cancel
    MsgBox "No file selected."
    Exit Sub
Else
    Set wb1 = Workbooks.Open(Filename:=NewFN)
End If
If NewFN2 = False Then
    ' They pressed Cancel
    MsgBox "No file selected."
    Exit Sub
Else
    Set wb2 = Workbooks.Open(Filename:=NewFN2)
End If
 
wb1.activate
wb1.Sheets(1).Activate
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,504
Messages
6,179,142
Members
452,892
Latest member
JUSTOUTOFMYREACH

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