Create Named Range for each column based on column heading text.

anilsharaf

New Member
Joined
Apr 8, 2014
Messages
43
Office Version
  1. 2007
Platform
  1. Windows
Create Named Range for more than 10 columns data. RangeName Based on Column Heading by iterating throuh each column Heading Like Range.(Cells(1,col). And Range Reference to give by iterating throug each column from row 2 to Last row. Also Add these names to Thisworkbook.Worksheets("AllotedRollNos"). Data Table is:
Sitting Board_V1 Principle and Module InProcess.xlsm
ABCDEFGHIJKLMN
1Sn Final according to Check ListSort1 Medium Hindi 1 then English 2Sort2 Type Regular 1 or Div Improve 2 Sort3 Center Number MediumTypeSchool Name Hss 2024Sort Temp Continuous Roll NoRN fromRN Tosort4 according to strength for sitting PriorityTotal StudentsSn FromSn To
2111321042HindiRegularBarpali1224320378622432038303450145
3211321044HindiRegularBoys22243203831224320389916946114
4311321070HindiRegularMadwa422432039462243203981436115150
5411321224HindiRegularPithampur522432039822243204046265151215
6511322292HindiRegularDVM722432041152243204139525216240
7612321044HindiDiv ImproveBoys922432800212243280021601241241
8712321070HindiDiv ImproveMadwa822432990022243299002701242242
9821321044EnglishRegularAtmanand322432039002243203945946243288
10921322071EnglishRegularLions622432040472243204114868289356
111022322071EnglishDiv ImproveLions10224328002222432800221001357357
12Tot357
AllotedRollNos

VBA Code:
Sub CreateNamedRanges_NotReady()
    Dim ws As Worksheet
    Dim LastRow As Long
    Dim Col As Long
    Dim TempRng As Range
    Dim Header As String
    Dim RangeName As String
    
    ' Set the worksheet where your data is located
    Set ws = ThisWorkbook.Worksheets("AllotedRollNos") ' Change "AllotedRollNos" to your sheet name
    
    ' Find the last row in column A (assuming your data starts from row 2)
    LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
    ' Loop through columns A to V
    For Col = 1 To 22 ' Adjust the range as needed
        ' Get the header text from row 1
        Header = ws.Cells(1, Col).Value
        
        ' Create the range name
        RangeName = Header
        
        ' Define the range (from row 2 to LastRow)
        With ThisWorkbook.Worksheets("AllotedRollNos")
        'Set ws.Range(ws.Cells(2, Col), ws.Cells(LastRow, Col)).Name = RangeName
        Set TempRng = ws.Range(ws.Cells(2, Col), ws.Cells(LastRow, Col))
        PrintTest = "=" & TempRng.Address
        'ThisWorkbook.Names.Add Name:=RangeName, RefersTo:="A2:A11": 'ws.Range(TempRng.Address)"
        
        '********
        ActiveWorkbook.Worksheets("AllotedRollNos").Names.Add Name:= _
        RangeName, RefersTo:="=" & TempRng.Address
        '********
        End With
        
    Next Col
End Sub
I tried many variations. But at the time of test all failed. Line 33 between stars create runtime error 1004. Thanks in advance
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try
VBA Code:
       ActiveWorkbook.Worksheets("AllotedRollNos").Names.Add Name:= _
        RangeName, RefersTo:= TempRng
 
Upvote 0
Try
VBA Code:
       ActiveWorkbook.Worksheets("AllotedRollNos").Names.Add Name:= _
        RangeName, RefersTo:= TempRng
It is again showing runtime error 1004
1712968782092.png
 
Upvote 0
Edited: The names are the issue.
  • A name cannot be more than 255 characters long.
  • You cannot use spaces in your names.
  • The first letter can only be a letter, an underscore, or a backslash.
  • You cannot use actual cell addresses as named ranges. For example, you cannot call your named range “A7”.
  • You can use symbols such as forward slashes, underscores and dots in names, but you cannot use other symbols.
 
Last edited:
Upvote 1
Solution
Edited: The names are the issue.
  • A name cannot be more than 255 characters long.
  • You cannot use spaces in your names.
  • The first letter can only be a letter, an underscore, or a backslash.
  • You cannot use actual cell addresses as named ranges. For example, you cannot call your named range “A7”.
  • You can use symbols such as forward slashes, underscores and dots in names, but you cannot use other symbols.
Excellent! Thank You so much. This worked fine. I replaced all spaces with underscore of column headings and it was solved. Like Below:
Sitting Board_V1 Principle and Module InProcess.xlsm
ABCDEFGHIJKLMN
1Sn_Final_according_to_Check_ListSort1_Medium_Hindi_1_then_English_2Sort2_Type_Regular_1_or_Div_Improve_2_Sort3_Center_Number_MediumTypeSchool_Name_Hss_2024Sort_Temp_Continuous_Roll_NoRN_fromRN_Tosort4_according_to_strength_for_sitting_PriorityTotal_StudentsSn_FromSn_To
2111321042HindiRegularBarpali1224320378622432038303450145
3211321044HindiRegularBoys22243203831224320389916946114
4311321070HindiRegularMadwa422432039462243203981436115150
5411321224HindiRegularPithampur522432039822243204046265151215
6511322292HindiRegularDVM722432041152243204139525216240
7612321044HindiDiv ImproveBoys922432800212243280021601241241
8712321070HindiDiv ImproveMadwa822432990022243299002701242242
9821321044EnglishRegularAtmanand322432039002243203945946243288
10921322071EnglishRegularLions622432040472243204114868289356
111022322071EnglishDiv ImproveLions10224328002222432800221001357357
12Tot357
AllotedRollNos

Again Thanks!!!
 
Upvote 0

Forum statistics

Threads
1,215,223
Messages
6,123,711
Members
449,118
Latest member
MichealRed

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