Solved

Define named ranges while copying worksheets

Posted on 2011-03-23
4
444 Views
Last Modified: 2012-05-11
I have a VBA script that copies active ranges in worksheets to corresponding worksheets in a new Workbook. -

For Counter = 1 To 7   ' number of worksheets to copy
        SourceWb.Worksheets(Counter).UsedRange.Copy DestWb.Worksheets(Counter).[a1]  
 ' get UsedRange from each source worksheet and copy to destination
    Next

What I need to do is modify this to set a named range for each Worksheet on the destination Workbook that only includes the active range that was copied.  Ideally I'd like to name the range 'worksheet name'_range.  I have PivotTables and charts on the destination Workbook that reference the named ranges.

0
Comment
Question by:Ed_CLP
  • 2
4 Comments
 

Expert Comment

by:NigelBulley
ID: 35202563
This is a clever bit of code for adding variablised Excel ranges but I do not fully understand your requirements. I hope this dynamic subroutine helps :-


Public Sub(intRowStart, intColStart, intRowEnd,intColEnd,Counter, strRangeName)

ActiveWorkbook.Names.Add Name:=strRangeName, RefersToR1C1:= _
"=" & Worksheets(Counter).Name & "!R" & Cstr(intRowStart) & "C" & Cstr(intColStart) & ":R" & CStr(intRowEnd) & "C" & intColEnd

End Sub

The sub routine was derived from the following macro recorded when adding a new named range :-

ActiveWorkbook.Names.Add Name:="new_name", RefersToR1C1:= _
"=Sheet1!R1C1:Rnew_name_counterC2"

If you enjoy electronic music please visit my music site which has between the www and com the following name areasontomakemusic.

Thats all folks
0
 

Expert Comment

by:NigelBulley
ID: 35202570
Darn it , I meant

Public Sub AddDynamicRange(intRowStart, intColStart, intRowEnd,intColEnd,Counter, strRangeName)

ActiveWorkbook.Names.Add Name:=strRangeName, RefersToR1C1:= _
"=" & Worksheets(Counter).Name & "!R" & Cstr(intRowStart) & "C" & Cstr(intColStart) & ":R" & CStr(intRowEnd) & "C" & intColEnd

End Sub
0
 
LVL 6

Accepted Solution

by:
rbrhodes earned 500 total points
ID: 35202681
Her you go

Sub XferAndName()

Dim Ref As String
Dim Naem As String
Dim Addr As String
Dim destWB As Workbook
Dim sourceWB As Workbook

    Set sourceWB = Workbooks("book2")
    Set destWB = Workbooks("book1")

    'Number of worksheets to copy
    For Counter = 1 To 3
        'Get UsedRange from each source worksheet and copy to destination
        sourceWB.Worksheets(Counter).UsedRange.Copy destWB.Worksheets(Counter).[a1]
        'Create name for range
        Addr = sourceWB.Worksheets(Counter).UsedRange.Address(False, False)
        Addr = Application.WorksheetFunction.Substitute(Addr, ":", "_")
        Naem = destWB.Worksheets(Counter).Name & "_" & Addr
        'create name for refers to
        Ref = "='" & destWB.Worksheets(Counter).Name & "'!" & sourceWB.Worksheets(Counter).UsedRange.Address
        'Do name
        destWB.Names.Add Name:=Naem, RefersTo:=Ref
    Next

    'Cleanup
    Set destWB = Nothing
    Set sourceWB = Nothing

End Sub
0
 

Author Closing Comment

by:Ed_CLP
ID: 35202905
Perfect solution!!!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

813 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

13 Experts available now in Live!

Get 1:1 Help Now