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
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
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
 
Upvote 0
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..."
 
Upvote 0
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:
Upvote 0
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
 
Upvote 0
You cannot name a Range like that.
Named Ranges cannot be named 1456 for example.
At least as far as I know
 
Last edited:
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,534
Messages
6,114,186
Members
448,554
Latest member
Gleisner2

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