Solved

Copy named range into another worksheet and link the values

Posted on 2011-09-04
4
220 Views
Last Modified: 2012-05-12
Dear Experts:

I would like to achieve the following using VBA:

Copy the named range (workbook level name = MyRange) of the worksheet named 'Data' to the worksheet named 'Results' with the following requirements...

.... Link the values
.... Start the pasting action on cell reference A5 of the 'Results' worksheet.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
0
Comment
Question by:AndreasHermle
  • 2
  • 2
4 Comments
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36481297
One way of doing this is to enter and array formula referencing the name in the Results sheet, as follows:

Sub CopyRange()
    
    Dim wbk As Excel.Workbook
    Dim rngSource As Excel.Range
    Dim rngTarget As Excel.Range
    
    Set wbk = ActiveWorkbook

    Set rngSource = Application.Range("MyRange")
    Set rngTarget = wbk.Worksheets("Results").Cells(1, 5)
    Set rngTarget = rngTarget.Resize(rngSource.Rows.Count, rngSource.Columns.Count)
    
    rngTarget.FormulaArray = "=MyRange"

End Sub

Open in new window

0
 

Author Comment

by:AndreasHermle
ID: 36482598
Hi andrew:

thank you very much for your professional support. This macro works great and will come in handy for me in other applications.

But regrettably I will have to move some parts of the copied array to other locations in the destination sheet and this results in the 'error' message:

'you cannot change part of an array'

So I would be pretty happy if you or somebody else could provide me with another solution to my requirements.

But again, thank you very much for your great support. Your approach is a neat one.

Regards, Andreas
0
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 500 total points
ID: 36482793
I don't like this one so much, but it gives a non-array solution:

Sub CopyRange()
    
    Dim wbk As Excel.Workbook
    Dim rngSource As Excel.Range
    Dim rngTarget As Excel.Range
    
    Set wbk = ActiveWorkbook

    Set rngSource = Application.Range("MyRange")
    Set rngTarget = wbk.Worksheets("Results").Cells(5, 1)
    
    rngSource.Copy
    rngTarget.Worksheet.Activate
    rngTarget.Select
    rngTarget.Worksheet.Paste link:=True
    
End Sub

Open in new window

0
 

Author Comment

by:AndreasHermle
ID: 36485625
Hi andrew:

works like a charm. Thank you very much for your professional help. Regards, Andreas
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now