Hi Vinoth
This code is in the attached and accommodates this
The other issue is that the headers such as revenues, selling expenses etc will change from time to time but in your code it appears to be defined (within the array function). Is there a way to make this dynamic?
' Adapted from http://stackoverflow.com/questions/6775165/collect-all-names-in-a-column-and-put-them-in-an-array-in-excel
Option Explicit
Sub Create_Named_Ranges()
Dim i As Long
Dim LC As Long
Dim HeaderList As Range
Dim cUnique As New Collection
Dim Header() As String
Dim lMatch As Long
Dim lngHdr As Long
Dim rng As Range
With Sheets("Database")
LC = .Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
Set HeaderList = .Range(.Cells(1, 3), .Cells(1, LC))
'Loop over every column and add the value to the collection (with unique key)
For i = 1 To HeaderList.Columns.Count
On Error Resume Next
If Not IsEmpty(HeaderList(1, i)) Then
cUnique.Add HeaderList(1, i), CStr(HeaderList(1, i))
End If
Next i
'Store back the value from the collection to an array
ReDim Header(1 To cUnique.Count, 1 To 1)
For i = 1 To cUnique.Count
Header(i, 1) = cUnique(i)
Next i
End With
For lngHdr = LBound(Header) To UBound(Header) Step 1
lMatch = Application.Match(Header(lngHdr, 1), Sheets("Database").Rows(1), 0)
If IsNumeric(lMatch) Then
Set rng = Cells(1, lMatch).Offset(2, 0).Resize(Range("B1"), Range("B2"))
ActiveWorkbook.Names.Add Name:=Header(lngHdr, 1), refersto:=rng
End If
Next lngHdr
End Sub
Let me know of issues.
Bookmarks