Page 1 of 2 12 LastLast
Results 1 to 10 of 11

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

  1. #1
    New Member
    Join Date
    Oct 2019
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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

  2. #2
    Board Regular My Aswer Is This's Avatar
    Join Date
    Jul 2014
    Posts
    15,833
    Post Thanks / Like
    Mentioned
    28 Post(s)
    Tagged
    13 Thread(s)

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

    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
    Be sure and always test this script on sample data the first time you use this as to avoid any problems with your data. Always trying to learn more and help others where I can. I'm using Excel 2013.
    Patience please I知 not perfect yet. "Memories are forever"

  3. #3
    New Member
    Join Date
    Oct 2019
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    Quote Originally Posted by My Aswer Is This View Post
    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..."

  4. #4
    Board Regular My Aswer Is This's Avatar
    Join Date
    Jul 2014
    Posts
    15,833
    Post Thanks / Like
    Mentioned
    28 Post(s)
    Tagged
    13 Thread(s)

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

    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 by My Aswer Is This; Oct 8th, 2019 at 08:09 AM.
    Be sure and always test this script on sample data the first time you use this as to avoid any problems with your data. Always trying to learn more and help others where I can. I'm using Excel 2013.
    Patience please I知 not perfect yet. "Memories are forever"

  5. #5
    New Member
    Join Date
    Oct 2019
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    The data is cell A2 is a formula, but is displaying a 4 digit code (e.g. 0018)

  6. #6
    New Member
    Join Date
    Oct 2019
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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

  7. #7
    Board Regular My Aswer Is This's Avatar
    Join Date
    Jul 2014
    Posts
    15,833
    Post Thanks / Like
    Mentioned
    28 Post(s)
    Tagged
    13 Thread(s)

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

    You cannot name a Range like that.
    Named Ranges cannot be named 1456 for example.
    At least as far as I know
    Last edited by My Aswer Is This; Oct 8th, 2019 at 08:32 AM.
    Be sure and always test this script on sample data the first time you use this as to avoid any problems with your data. Always trying to learn more and help others where I can. I'm using Excel 2013.
    Patience please I知 not perfect yet. "Memories are forever"

  8. #8
    Board Regular My Aswer Is This's Avatar
    Join Date
    Jul 2014
    Posts
    15,833
    Post Thanks / Like
    Mentioned
    28 Post(s)
    Tagged
    13 Thread(s)

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

    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.
    Be sure and always test this script on sample data the first time you use this as to avoid any problems with your data. Always trying to learn more and help others where I can. I'm using Excel 2013.
    Patience please I知 not perfect yet. "Memories are forever"

  9. #9
    Board Regular My Aswer Is This's Avatar
    Join Date
    Jul 2014
    Posts
    15,833
    Post Thanks / Like
    Mentioned
    28 Post(s)
    Tagged
    13 Thread(s)

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

    Show me what you have in B2
    Be sure and always test this script on sample data the first time you use this as to avoid any problems with your data. Always trying to learn more and help others where I can. I'm using Excel 2013.
    Patience please I知 not perfect yet. "Memories are forever"

  10. #10
    New Member
    Join Date
    Oct 2019
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •