Define named ranges while copying worksheets

Posted on 2011-03-23
Medium Priority
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

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.

Question by:Ed_CLP
  • 2

Expert Comment

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:= _

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

Thats all folks

Expert Comment

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

Accepted Solution

rbrhodes earned 2000 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

    Set destWB = Nothing
    Set sourceWB = Nothing

End Sub

Author Closing Comment

ID: 35202905
Perfect solution!!!

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
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…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

839 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