VBA: Copy data from closed Workbook and paste

cozzagiorgi

New Member
Joined
Jun 27, 2018
Messages
41
Hi

I am using this code to copy data from a closed workbook:

Code:
Sub GetDataFromClosedBook()
    Dim mydata As String
    
    'data location & range to copy
    mydata = "='F:\filepath\filename.xlsm'!NamedRange" '<< change as required


    'link to worksheet
    With ThisWorkbook.Worksheets(1).Range("B10:M23")
        .Value = mydata
        'convert formula to text
        .Value = .Value
    
    End With
End Sub

It works fine as long as the line:

Code:
With ThisWorkbook.Worksheets(1).Range("B10:M23")

is equal in size as the size of the named range in mydata AND it is at the same spot as the original named range.

How can I put mydata in other ranges and adapt the
Code:
Range("B10:M23")
to the size of the original named range?

I hope I am clear, english isn't my mother tongue. Ask away if necessary. Thanks!
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
The following macro opens the source workbook, and then copies the values from the named range to the destination workbook, starting at B2. Change the starting cell as desired. Then, once the values have been copied, it closes the workbook.

Note, however, ScreenUpdating is set to False prior to opening the workbook. So the workbook will be opened in the background and won't be visible to the user. ScreenUpdating is then set to False at the end of the macro.

Code:
Option Explicit

Sub GetDataFromClosedBook()


    Dim pathToFile As String
    pathToFile = "F:\filepath\"
    If Right(pathToFile, 1) <> "\" Then
        pathToFile = pathToFile & "\"
    End If


    Dim fileName As String
    fileName = "filename.xlsm"
    
    Dim rangeName As String
    rangeName = "NamedRange"
    
    Application.ScreenUpdating = False
    
    Dim sourceWorkbook As Workbook
    Set sourceWorkbook = Workbooks.Open(fileName:=pathToFile & fileName, ReadOnly:=True)
    
    Dim sourceRange As Range
    Set sourceRange = sourceWorkbook.Names(rangeName).RefersToRange
    
    With sourceRange
        ThisWorkbook.Worksheets(1).Range("B2").Resize(.Rows.Count, .Columns.Count).Value = .Value
    End With
    
    sourceWorkbook.Close SaveChanges:=False
    
    Application.ScreenUpdating = True


End Sub

Hope this helps!
 
Upvote 0
Hi

Thanks for your answer. I'd rather don't want to open the workbooks as that would slow down my application.

In the end, I will use this to copy data from several workbooks in a loop, so speed has to be kept up.
 
Upvote 0

Forum statistics

Threads
1,214,574
Messages
6,120,327
Members
448,956
Latest member
Adamsxl

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