Solved

Define named ranges while copying worksheets

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
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 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 demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

867 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

22 Experts available now in Live!

Get 1:1 Help Now