Solved

Define named ranges while copying worksheets

Posted on 2011-03-23
4
442 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
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 …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

744 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

11 Experts available now in Live!

Get 1:1 Help Now