LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #5   Report Post  
Posted to microsoft.public.excel.misc,microsoft.public.excel.programming
external usenet poster
 
Posts: 345
Default using the same range name on different worksheets in a workboo

Paul,
I'm far from an MVP but am going thru kinda the
same thing.

Named ranges are @ workbook level, cannot duplicate them.
I tried. Putting in the same name again overrides the first.

You'll need an "identification" section in your general code.

Below has not not been tested but should be very close to working.
The function at the end works. I use it.
You'll be dealing with the Workbook.Names collection and the
.RefersTo property.

1st sheet, Ws1 Named range "RangeOne" =Ws1!$A$1:$D$1 4 cells row 1
2nd sheet, Ws2 Named range "RangeTwo" =Ws2!$E$9:$H$9 4 cells row 9

Sub General()

Dim Ws as worksheet, NamedRange as string, DataRng as range
dim CellsAdr as string

if activesheet.name = "ws1" then
set ws = sheets("ws1")
NamedRange = "RangeOne"

elseif activesheet.name = "ws2" then
set ws = sheets("ws2")
NamedRange = "RangeTwo"
end if


CellsAdr = _
sCellAdr_vsRefToF(Activeworkbook.names(NamedRange) .RefersTo)
'Above gets "pure" address without the sheet reference

set datarng = Ws.range(cellsadr) '4 cells worth of data from wherever

'Your code. You'll have to get the answer back to the correct Ws.
' If Ws.Name = "Ws1" then .....

End sub


Public Function sCellAdr_vsRefToF(sRefersTo As String) As String

'Return plain address portion from "=Ws1!$A$1:$D$1 .Names(xxxx).RefersTo
'areas will work too "=Ws1!$A$1:$D$1,!$H$8:$J$10

Dim Text As String, iByte As Integer

iByte = InStr(sRefersTo, "!")

Text = Right(sRefersTo, Len(sRefersTo) - iByte)

Text = Replace(Text, "!", "")

sCellAdr_vsRefToF = Text

End Function
--
Neal Z


"Paul" wrote:

I didn't mean to include the opening sentence in my original post - here is
the correct wording for my question. (sorry for any confusion):

I'm building an application that has multiple worksheets in a workbook, and
I have written some VBA code in a general module that I would like to be
able to use in each of the worksheets.

Part of the code performs actions based on the Range names of certain cells
in the worksheet. Since I would like to be able to use this same code to
perform the same operations in the various worksheets, I would like to be
able to create the same Range name to corresponding cells in each workbook,
so the code will perform the same actions in each of the worksheets. (I
need to assign Range names because the "corresponding" cells aren't always
in the same Row).

However, I have noticed that if I try to use a Range name in one worksheet
that has already been defined in another worksheet, it won't let you do it.

Is there any way I can give cells in two different worksheets the same Range
name, so the same VBA code can be used to perform the same operations in the
different worksheets?

Thanks in advance,

Paul





 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Combine worksheets in multiple workbook in one workbook with a macro Sam Commar Excel Discussion (Misc queries) 2 April 2nd 09 01:09 PM
Named Range From One Workbook Used in Validation Drop Down in 2nd Workbook Minitman Excel Discussion (Misc queries) 3 August 19th 08 05:30 PM
Create New Workbook from Worksheets in One Workbook Dave Excel Discussion (Misc queries) 4 June 17th 07 10:27 PM
Sum same cell/range of multiple worksheets within a workbook... geld Excel Worksheet Functions 3 January 5th 07 05:15 AM
How do I build a workbook from the worksheets another workbook? Rico Excel Discussion (Misc queries) 4 August 19th 05 02:04 PM


All times are GMT +1. The time now is 11:56 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"