Opening another workbook and copying data

attikuz

New Member
Joined
Jul 23, 2013
Messages
26
Office Version
  1. 365
Hello,

I am trying to create a macro that opens another file, copies specific cells, and then pastes onto my current workbook. I can't seem to define the new workbook when it opens, can someone please tell me where my code is going wrong? Thanks

VBA Code:
Sub Macro1()
'
' Update LED Table Macro - this macro will open and copy the data from the LED Strategic tab into the workbook
    Dim wbCopy As Worksheet
    Dim wbDest As Worksheet
    Set wbDest = ThisWorkbook.Sheets("Data")
    
' Open the GB AA Spreadsheet
    Dim fd As Office.FileDialog
    Dim strFile As String
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
    .Filters.Clear
    .Filters.Add "Excel Files", "*.xlsx?", 1
    .Title = "Choose an Excel file"
    .AllowMultiSelect = False
    .InitialFileName = "Q:\Bri\Greenbank\Investment\Asset Allocation\"
    If .Show = True Then
        strFile = .SelectedItems(1)
     End If
    End With
    Workbooks.Open (strFile)
    Set wbCopy = ThisWorkbook.Sheets("LED Strategic")

'Copy the Cells
    wbCopy.Range("I3,N3,S3,X3,AC3,I10:I14,N10:N14,S10:S14,X10:X14,AC10:AC14,I24,N24,S24,X24,AC24").Copy
    wbDest.Range("H2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
    
'Close the GB AA Spreadsheet
    wbCopy.Close
    
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

ChrisFoster

Board Regular
Joined
Jun 21, 2019
Messages
160
Office Version
  1. 365
Platform
  1. Windows
When you run this code do you get a debug error? If so on what line?
 

attikuz

New Member
Joined
Jul 23, 2013
Messages
26
Office Version
  1. 365
When you run this code do you get a debug error? If so on what line?

Yes, i get it towards the end on this line:

VBA Code:
Set wbCopy = ThisWorkbook.Sheets("LED Strategic")

It is a Run-Time error '9' FYI
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
73,163
Office Version
  1. 365
Platform
  1. Windows
Is that sheet in the workbook containing the code, or the workbook you just opened?
 

attikuz

New Member
Joined
Jul 23, 2013
Messages
26
Office Version
  1. 365
Is that sheet in the workbook containing the code, or the workbook you just opened?

My bad! It works changing it to activeworkbook!

It dosen't close though if i put

wbcopy.close at the end - just get an error?
 

ChrisFoster

Board Regular
Joined
Jun 21, 2019
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Use the below to close the active workbook

Code:
ActiveWorkbook.Close Savechanges:=False
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,855
Messages
5,833,998
Members
430,250
Latest member
Reggie Mcqueen

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
Top