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

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
When you run this code do you get a debug error? If so on what line?
 
Upvote 0
Is that sheet in the workbook containing the code, or the workbook you just opened?
 
Upvote 0
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?
 
Upvote 0
Use the below to close the active workbook

Code:
ActiveWorkbook.Close Savechanges:=False
 
Upvote 0

Forum statistics

Threads
1,212,931
Messages
6,110,745
Members
448,295
Latest member
Uzair Tahir Khan

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