Dynamic Named Ranges

Risk

Board Regular
Joined
Jul 27, 2006
Messages
71
Hi,

I'm trying to set dynamic named ranges using vba instead of the offset option I've seen on some forums. Here is what I have so far, but I'm running into a problem with the last two named ranges that reference the LastColumn and DataRng. I'm definitely a beginning in vba so go easy on my code, it took my 4 hours to write with trial and error, but please feel free to point out better methods and what is wrong with the code.

Thanks in advance,
Risk

Code:
Sub ConstructionNamedRange()

Dim LastRow As Long
Dim LastColumn As Integer
Dim DataRng As Range

Worksheets("Construction").Activate
LastRow = Cells.Find(What:="*", After:=Range("A1"), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False).Row
LastColumn = Cells.Find(What:="*", After:=Range("A1"), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False).Column

Set DataRng = Range(Cells("1", "A"), Cells(LastRow, LastColumn))

    '''Names Site
    ThisWorkbook.Names.Add Name:="c_Site", _
    RefersTo:=Worksheets("Construction").Range("A1:A" & LastRow)
    
    '''Names Type
    ThisWorkbook.Names.Add Name:="c_Type", _
    RefersTo:=Worksheets("Construction").Range("B1:B" & LastRow)
    
    '''Names Category
    ThisWorkbook.Names.Add Name:="c_Category", _
    RefersTo:=Worksheets("Construction").Range("C1:C" & LastRow)
    
    '''Names New Construction
    ThisWorkbook.Names.Add Name:="c_Construction", _
    RefersTo:=Worksheets("Construction").Range("D1:D" & LastRow)
     
    '''Names Forecast
    ThisWorkbook.Names.Add Name:="c_Forecast", _
    RefersTo:=Worksheets("Construction").Range("E1:E" & LastRow)
    
    '''*****************************************************'''
        '''**** Problem starts here****'''
    '''*****************************************************'''
    
    '''Names Header
    ThisWorkbook.Names.Add Name:="c_header",
    RefersTo:=Worksheets("Construction").Range("A1:" & LastColumn & "1")
    
    '''Names Data Range
    ThisWorkbook.Names.Add Name:="c_Data", _
    RefersTo:=Worksheets("Construction").Range(DataRng)

End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
There is a non volatle native excel solution.
Name: c_Site
RefersTo: INDEX(Construction!$A:$A, 1, 1):INDEX(Construction!$A:$A, MATCH(REPT("z",255),Construction!$A:A), 1)

(If you anticipate the last entry in column A to be numeric, change the REPT("z",255) to 9E+314.
)

Name: c_Type
RefersTo: INDEX(Construction!$B:$B, 1, 1):INDEX(Construction!$B:$B, ROWS(c_Site), 1)

etc.

As to your VB, this should take care of c_Header
Code:
ThisWorkbook.Worksheets("Construction").Range("A1").Resize(1, LastColumn).Name = "c_header"

Its not clear to me what the issue with DataRange is.
 
Last edited:
Upvote 0
Mikerickson,

Thanks for your help. The fix works

Code:
ThisWorkbook.Worksheets("Construction").Range("A1").Resize(1, LastColumn).Name = "c_header"

using your string, I was able to correct the data range

Code:
ThisWorkbook.Worksheets("Construction").Range("A1").Resize(LastRow, LastColumn).Name = "c_Data"

I'm still playing around with
There is a non volatile native excel solution.
Name: c_Site
RefersTo: INDEX(Construction!$A:$A, 1, 1):INDEX(Construction!$A:$A, MATCH(REPT("z",255),Construction!$A:A), 1)

(If you anticipate the last entry in column A to be numeric, change the REPT("z",255) to 9E+314.
)

Name: c_Type
RefersTo: INDEX(Construction!$B:$B, 1, 1):INDEX(Construction!$B:$B, ROWS(c_Site), 1)

It seems to work, though hides it in the name box as if Visible:=False. Then again maybe I'm doing something wrong. Thank you for your help, I appreciate you showing both the native and VBA solutions.

-Risk
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,730
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top