Opening another workbook and copying data

attikuz

New Member
Joined
Jul 23, 2013
Messages
15
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
 

Some videos you may like

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

ChrisFoster

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

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,343
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
15
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
127
Office Version
  1. 365
Platform
  1. Windows
Use the below to close the active workbook

Code:
ActiveWorkbook.Close Savechanges:=False
 

Watch MrExcel Video

Forum statistics

Threads
1,123,448
Messages
5,601,713
Members
414,470
Latest member
glukemey

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