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,066
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,066
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,066
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,066
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,077,959
Messages
5,337,413
Members
399,144
Latest member
Lauren Ward

Some videos you may like

This Week's Hot Topics

Top