Managing Range Names With VBA
Managing Range Names With VBA
Managing Range Names With VBA
Page 1 of 4
This will create the name, using the current selection as the reference
Sub MakeName_Selection() Sheets("Sheet2").Activate Range("A1").CurrentRegion.Select ActiveWorkbook.Names.Add Name:="PTable", RefersTo:=Selection End Sub
Let's assume that you need to find a specific cell first, then create a range 60 columns wide, offset from the found cell. You could use code like this:
'find row for Lot Profile Range("A1").Select Cells.Find(What:="Lot Profile", After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False).Activate ActiveCell.Offset(1,5).Resize(1,60).Select ActiveWorkbook.Names.Add Name:="Lots", RefersTo:=Selection 'you can also create names based on an offset of the current selection: ActiveWorkbook.Names.Add Name:="Deposits", RefersTo:=Selection.Offset(1,0)
http://www.datawright.com.au/excel_resources/excel_range_names_vba.htm 11/21/2012
Page 2 of 4
Sub NameProperties() Dim nm As Name Set nm = Names("Sheetlist") On Error Resume Next With nm Debug.Print "Category: " & nm.Category 'valid only with XLM macros Debug.Print "CategoryLocal: " & nm.CategoryLocal Debug.Print "Creator: " & nm.Creator Debug.Print "Comment: " & nm.Comment 'new in Excel 2007 Debug.Print "Index: " & nm.Index Debug.Print "MacroType: " & nm.MacroType 'valid only with XLM macros. Returns xlNone ot Debug.Print "Name: " & nm.Name Debug.Print "NameLocal: " & nm.NameLocal Debug.Print "Parent: " & nm.Parent Debug.Print "RefersTo: " & nm.RefersTo Debug.Print "RefersToLocal: " & nm.RefersToLocal Debug.Print "RefersToR1C1: " & nm.RefersToR1C1 Debug.Print "RefersToR1C1Local: " & nm.RefersToR1C1Local Debug.Print "RefersToRange: " & nm.RefersToRange Debug.Print "ShortcutKey: " & nm.ShortcutKey 'valid only with XLM macros Debug.Print "ValidWorkbookParameter: " & nm.ValidWorkbookParameter 'new in Excel 2007 Debug.Print "Value: " & nm.Value Debug.Print "Visible: " & nm.Visible Debug.Print "WorkbookParameter: " & nm.WorkbookParameter 'new in Excel 2007 End With End Sub
Value Creator 1480803660 (code for XLCreator) Index 1327 MacroType -4142 (code for xlNone) Name Sheetlist NameLocal Sheetlist RefersTo =OFFSET(Scenario!$D$4,0,0,COUNTA(Scenario!$D:$D)-1,1) RefersToLocal =OFFSET(Scenario!$D$4,0,0,COUNTA(Scenario!$D:$D)-1,1) RefersToR1C1 =OFFSET(Scenario!R4C4,0,0,COUNTA(Scenario!C4)-1,1) RefersToR1C1Local =OFFSET(Scenario!R4C4,0,0,COUNTA(Scenario!C4)-1,1) ValidWorkbookParameter False Value =OFFSET(Scenario!$D$4,0,0,COUNTA(Scenario!$D:$D)-1,1) Visible True WorkbookParameter False
Property
http://www.datawright.com.au/excel_resources/excel_range_names_vba.htm 11/21/2012
Page 3 of 4
Assuming that you ran this on a sheet called Chart Data, with NSW Sales in Column D, the corresponding range name would be called NSW_Sales. Its reference as created by the above code would be
=OFFSET('Chart Data'!$D$2,0,0,COUNTA('Chart Data'!$D:$D)-1,1)
http://www.datawright.com.au/excel_resources/excel_range_names_vba.htm 11/21/2012
Page 4 of 4
http://www.datawright.com.au/excel_resources/excel_range_names_vba.htm 11/21/2012