Using VBA to use the value of a Named Range in one Workbook to define the Workbook ("Name") for another Workbook

Lexcon07

New Member
Joined
Apr 21, 2020
Messages
36
Office Version
  1. 365
Platform
  1. Windows
I'm trying to use VBA to copy and paste a range of cell from one workbook to another while being able to easily change the name of the destination file.

What I am trying to do is use a Named Range/Cell (i.e. "DestinationFileName") in the original Excel file (i.e. "Source File.xlsm") to define the name of the file I want to paste the information to.

For example, in the script below, I am copy data from the "Source File.xlsm" workbook and pasting it to the "Destination File.xlsm" workbook. I would like to use the Named Range/Cell (i.e. "DestinationFileName") in the "Source File.xlsm" workbook to define the name of the destination file.

VBA Code:
Sub ExportRange1()
    Application.ScreenUpdating = False
        Workbooks("Source File.xlsm").Worksheets("ExportSheet").Range("ExportRange1").Copy
        Workbooks("Destination File.xlsm").Worksheets("ImportSheet").Range("A2").PasteSpecial Paste:=xlPasteValues
    Application.ScreenUpdating = True
End Sub

Any help you can provide would be greatly appreciated.

Thanks in advance!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Can use shorter notation with named ranges

DestinationFile is a named range pointing at ONE cell whose value in my test was "File1.xlsx" (ie the file name INCLUDING the string)
- if the extenstion is not in the cell then amend dPath string like this
VBA Code:
dPath = "C:\folder\subfolder" &  "\" & [DestinationFile] & ".xlsx"

ExportRange is your existing named range

Set Source BEFORE opening destination file

Assigning values is faster than copy\paste if formatting etc is not required
(if otherwise, use your original notation)

Source.Copy : dWb.Sheets("ImportSheet").Range("A2")
VBA Code:
    Dim dWb As Workbook, dPath, Source As Range
    dPath = "C:\folder\subfolder" &  "\" & [DestinationFile]
    Set Source = [ExportRange]
    Set dWb = Workbooks.Open(dPath)
    dWb.Sheets("ImportSheet").Range("A2").Resize(Source.Rows.Count, Source.Columns.Count).Value = Source.Value
    dWb.Save
    dWb.Close False
 
Upvote 0
Hi Tongle,

Thank you for your help!

I had to make a couple modifications, but it's working now!
  • The DestinationFileName cell contains the complete file location, just in case the location of the files ever changes;
  • I was getting a error relating the line of code Set Source = [ExportRange], so I changed it to Set Source = Range("ExportRange") and it works now
VBA Code:
Sub ExportRange()
    Dim dWb As Workbook, dPath, Source As Range
    dPath = [DestinationFileName]
    Set Source = Range("ExportRange")
    Set dWb = Workbooks.Open(dPath)
    Set Destination = Workbooks.Open(dPath)
        ThisWorkbook.Worksheets("SourceData").Range("ExportRange").Copy
        Destination.Sheets("ImportSheet").Range("A2").PasteSpecial Paste:=xlPasteValues
    dWb.Save
End Sub

Thanks again!
 
Upvote 0
Solution

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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