Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Defining an R1C1 style dynamic named range using vba

Hi again,

Slight update - the code didnt work perfectly, it just appeared to
work perfectly. What is was doing was placing a formula in the named
ranges list, but it actually didnt refer to anything.

Anyway, here is the code that works:

Sub GenerateNamedRanges(TargetSheet As String)
Dim S As Worksheet
Dim WorkRange As Range
Dim i As Integer, CellCount As Integer
Dim Referral As String
Application.Volatile

Set S = ThisWorkbook.Worksheets(TargetSheet)
S.Activate

Set WorkRange = S.Rows(1).EntireRow
Set WorkRange = Intersect(WorkRange.Parent.UsedRange, WorkRange)

CellCount = WorkRange.Count

For i = CellCount To 1 Step -1
If Not IsEmpty(WorkRange(i)) Then
Referral = "=" & S.Name & "!R" & WorkRange(i).Row + 1 &
"C" & WorkRange(i).Column & ":"

Referral = Referral & "INDEX(" & S.Name & "!C" &
WorkRange(i).Column & ":" & "C" & WorkRange(i).Column & ","

Referral = Referral & "COUNTA(" & S.Name & "!C" &
WorkRange(i).Column & ":" & "C" & WorkRange(i).Column & "))"

ThisWorkbook.Names.Add Name:=WorkRange(i).Value,
RefersToR1C1:=Referral, Visible:=True
End If
Next i
End Sub

This will look at the first row of a worksheet, treat each cell with a
value in it as a range to be defined and use the cell value as the
name, and then pump the formula into the named range list for each
column. The named ranges are dynamic.

So in short - each column with a list of values starting in the
topmost cell will have a dynamic named range defined, the name of the
range will be whatever value is in the topmost cell, and the range is
dynamic starting from the second cell in the column and reaching down
to the last used cell.

Cheers

The Frog

Reply
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
How do I change sheet notation from R1C1 style to A1 style in XL 2 Sherlock1506 Setting up and Configuration of Excel 1 December 5th 06 03:22 PM
can a1 reference style and r1c1 style be used in same formula? rjagga Excel Worksheet Functions 1 September 17th 06 10:58 AM
Defining a named range for a dynamic result set Keith B.[_2_] Excel Programming 2 April 19th 06 10:26 PM
Defining Dynamic Range Jamie[_11_] Excel Programming 4 October 27th 05 06:36 PM
Dynamic Range in Macro with R1C1 Jonnyboy117 Excel Programming 3 July 9th 04 04:40 PM


All times are GMT +1. The time now is 01:20 PM.

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"