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
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