Moving between other workbooks in VBA

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
270
Office Version
  1. 365
Platform
  1. Windows
Hello,

The end user selects two files which populate two cells in my workbook, named ranges file_1 and file_2. These aren't just filenames, they also include the full filepath.

I want to start moving data between the two files but have come up with an "Object Required" error. This is my code, can anyone see what I'm doing wrong?
Code:
Sub open_files()

Application.ScreenUpdating = False

File_1 = Range("file_1").Value
File_2 = Range("file_2").Value
Workbooks.Open Filename:=File_1
Workbooks.Open Filename:=File_2
File_1.Activate '<----this line is the problem
Columns("A:A").Copy
End Sub

Thanks in advance!
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
File_1 is just text, not a workbook object. You should store the workbooks in variables:

VBA Code:
Sub open_files()

Application.ScreenUpdating = False

File_1 = Range("file_1").Value
File_2 = Range("file_2").Value
dim wb1 as workbook
set wb1 = Workbooks.Open(Filename:=File_1)
dim wb2 as workbook
set wb2 = Workbooks.Open(Filename:=File_2)
wb1.Activate 
Columns("A:A").Copy
End Sub
 
Upvote 0
For speed you should avoid activating the workbooks and just refer to their variable names:
VBA Code:
Option Explicit
Sub open_files()
    Dim wb1 As Workbook, wb2 As Workbook
    Dim File_1 As String, File_2 As String
    
    File_1 = Range("file_1").Value
    File_2 = Range("file_2").Value
    
    Set wb1 = Workbooks.Open(File_1)
    Set wb2 = Workbooks.Open(File_2)

    wb1.Sheets("Sheet1").Columns("A:A").Copy
    wb2.Sheets("Sheet1").Columns("A:A").PasteSpecial (xlAll)
    Application.CutCopyMode = False
End Sub
 
Upvote 0
Solution
Thanks so much both, both answers work perfectly but I can only mark one as the solution :(
 
Upvote 0

Forum statistics

Threads
1,215,087
Messages
6,123,050
Members
449,092
Latest member
ikke

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