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

milesy29

New Member
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
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
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
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
The data is cell A2 is a formula, but is displaying a 4 digit code (e.g. 0018)
 

milesy29

New Member
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
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
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
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
 

Some videos you may like

This Week's Hot Topics

  • Get External Data (long shot question!)
    This is likely a long shot but I am wondering if it is at all possible for Excel to somehow 'change' the contents of a URL that is being linked to...
  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • Cell Formatting
    Good Morning, I need to format a few different cells in the following manners: A1 has to always add a colon (:) after whatever is typed in by a...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • Workbook_Change stopped working !
    I am working on an app to speed up & automate processing of Credit Cards statements. After data is input from a CSV file, it is presented to the...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
Top