VBA to create a named range on each sheet with name of value in cell A2

milesy29

New Member
Joined
Oct 8, 2019
Messages
5
Hi there,

First time poster, but long term user of the help on this forum

I'm in need of some help. I have a workbook with 468 sheets of data. Each sheet has a table in the range of A4:AA500. I need to create a named range for each of the table ranges on every sheet and name it as per the value in cell A2 on that sheet to use in Power Query Editor.

So in the end, I would like 468 named ranges, uniquely named as per the cell value in cell A2 on each sheet.

Hope I've described that ok? Please let me know if you need any more clarification.

Any help would be greatly appreciated.

rich
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,080
Try this:
Code:
Sub Name_Range()
'Modified 10/8/2019 6:59:54 AM  EDT
Application.ScreenUpdating = False
Dim i As Long
For i = 1 To Sheets.Count
    With Sheets(i)
        .Range("A4:AA500").Name = Sheets(i).Range("A2").Value
    End With
Next
Application.ScreenUpdating = True
End Sub
 

milesy29

New Member
Joined
Oct 8, 2019
Messages
5
Try this:
Code:
Sub Name_Range()
'Modified 10/8/2019 6:59:54 AM  EDT
Application.ScreenUpdating = False
Dim i As Long
For i = 1 To Sheets.Count
    With Sheets(i)
        .Range("A4:AA500").Name = Sheets(i).Range("A2").Value
    End With
Next
Application.ScreenUpdating = True
End Sub
Thanks for the quick reply. I actually get an error with that for the line below

.Range("A4:AA500").Name = Sheets(i).Range("A2").Value

The error states "Run time error: '1004'. The syntax of this name isn't correct..."
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,080
Range names are tricky. Maybe you have a Value in A2 that may not be a proper Range Name.
Or maybe the Range("A2") has no value.

Like this value cannot be a Range Name:
"Cake-1"

Range names cannot have certain characters.

And my script goes through all sheets in the workbook.

Tell me how you decided on the values you have in Range("A2") of each sheet.
 
Last edited:

milesy29

New Member
Joined
Oct 8, 2019
Messages
5
The data is cell A2 is a formula, but is displaying a 4 digit code (e.g. 0018)
 

milesy29

New Member
Joined
Oct 8, 2019
Messages
5
Should add that cell B2 has text in the format of

'TEXT - TEXT (XXXX)

Then A2 has the formula

=MID(B2,FIND("(",B2)+1,FIND(")",B2)-FIND("(",B2)-1)

which is giving the result of

XXXX

and thats what I want to name the range of A4:AA500
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,080
You cannot name a Range like that.
Named Ranges cannot be named 1456 for example.
At least as far as I know
 
Last edited:

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,080
Why put a formula in A2?

Why not let the Vba script come up with the results.

Tell me what is the formula attempting to do.
 

milesy29

New Member
Joined
Oct 8, 2019
Messages
5
Ah of course! I've updated the text to remove the spaces, "-"'s and brackets and that now working. Thanks so much for your help
 

Forum statistics

Threads
1,078,501
Messages
5,340,751
Members
399,393
Latest member
farlow

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top