+ Reply to Thread
Results 1 to 12 of 12

Dynamically re-name a Named Range

  1. #1
    Registered User
    Join Date
    07-30-2012
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2007
    Posts
    11

    Dynamically re-name a Named Range

    I need to create a macro that will re-name a Named Range of cell A1 to match the text in cell A1 of each Worksheet. Cell A1 on each worksheet is composed of an "if" formula, which will change between the two options based on if the corresponding box is checked on the Checklist Worksheet.

    Here's my VB, which isn't working:
    Sub Dynamic_Range()
    Dim wkst As Worksheet
    For Each wkst In ActiveWorkbook.Worksheets
    wkst.Select
    Range("a1").Name = Range("a1")
    Next wkst
    End Sub

    I can get it to change the name of the named range to "addtopdf" but can't get it to change back to "check0", "check1" and "check2".

    If you look at the attached, it should be much clearer.

    Any help is greatly appreciated.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Dynamically re-name a Named Range

    Is this what you were after?
    Please Login or Register  to view this content.
    If your question has been satisfactorily addressed, please consider marking it solved. Click the Thread Tools dropdown and select Mark thread as solved.
    Also, you might want to add to the user's reputation by clicking the star icon in the lower left corner of the post with the answer- it's why we do what we do...

    Thomas Lafferty
    Analyst/Programmer

  3. #3
    Registered User
    Join Date
    07-30-2012
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Dynamically re-name a Named Range

    That didn't do the trick unfortunately. When I de-select the checkboxes on the first tab, and the cells in A1 on the other 3 tabs all change back to check1/2/3, their respective named ranges are still set to "addtopdf and not "check1" "check2" and "check3".

  4. #4
    Registered User
    Join Date
    07-30-2012
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Dynamically re-name a Named Range

    Any more takers out there? This one is seriously confounding me. Thanks.

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: Dynamically re-name a Named Range

    Try:

    Please Login or Register  to view this content.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  6. #6
    Registered User
    Join Date
    07-30-2012
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Dynamically re-name a Named Range

    That worked. Thank you, thank you, thank you!

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: Dynamically re-name a Named Range

    You're welcome. Thanks for the rep.

  8. #8
    Registered User
    Join Date
    07-30-2012
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Dynamically re-name a Named Range

    I guess I spoke a little too soon. The code works great in the simple workbook I attached at the top of this thread, but when I put it into a more complex workbook with several other macros, I get a run-time error '1004' (application-defined or object defined). My goal was to use this code to define Named Ranges of cell A1 on each worksheet by selecting or de-selecting a checkbox on the main tab. Once I can do that, then I can use some code I have compiles all worksheets with a certain named range into a PDF. That code works well too. It just seems to be that when I put the code above in my PDF workbook, I start getting the error (only on the code above though, not the PDF creation code).

    I thought it would be quite useful to just have a summary page where I could check boxes (one box per worksheet) and hit a button and then a PDF would be created that combines all the checked worksheets. If anyone has any further ideas, that would be great. If you need to see my other code, I can provide that. Thanks!

  9. #9
    Registered User
    Join Date
    07-30-2012
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Dynamically re-name a Named Range

    After some monkeying around, I was able to get it to work. The solution was to first delete all Named Ranges in the Workbook, and then use the modified code to set a Named Range for cell A1 on each worksheet to match the contents of the respective cell A1s. This is actually a pretty cool workbook now, allowing you to check a box for what portions you want included in a combined PDF file. If anyone is interested, I'll post the workbook with all the code in conjunction with eachother to be used.

    Here is the modified code for the named ranges portion:

    Sub Dynamic_Range()
    Dim sh As Worksheet
    Dim nm As Name

    On Error Resume Next
    For Each nm In ActiveWorkbook.Names
    nm.Delete
    Next
    On Error GoTo 0

    For Each sh In ActiveWorkbook.Worksheets
    With sh
    With .Range("a1")
    .Name = .Range("a1").Value
    End With
    End With
    Next sh

    End Sub

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: Dynamically re-name a Named Range

    If you were to add code to delete all named ranges to some of my workbooks, they would be critically and probably irretrievably broken

    I think you should consider being a little more selective.

    That said, I'm sure your workbook and code would be of interest so, yes, please share.


    Please note: to comply with forum rules, you should add Code Tags to your code extract.


    Regards, TMS

  11. #11
    Registered User
    Join Date
    07-30-2012
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Dynamically re-name a Named Range

    Good point and thank you again for your help. My workbook doesn't need Named Ranges other than for selection of which worksheets to print to PDF so the code is specific to that. I'll clean it up a bit a post. My apologies for missing the code tags. As you can tell, I'm very new to VBA and the VBA community but will make sure to do that in the future.

  12. #12
    Registered User
    Join Date
    07-30-2012
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Dynamically re-name a Named Range

    As promised, here is a clean version of the file. I removed all of the normal information on the tabs and one could just put whatever they wanted on each tab obviously.

    I use this to send our request for proposal documents to bidders. It comes in handy as I don't need all of the documents (each tab is its own document) most of the time.

    Things to note:
    - Leave cell A1 on each tab alone or the macros will error out.
    - The VBA code for creating the PDF and email came from Ron de bruin, who has his code publicly available all over the internet, including the Microsoft help website
    - I added some code (thanks for the help TMShucks) that allows for the cover page to just select which tabs to create a PDF from.
    - There is a private sub on one of the tabs. I just used this as one of the rows needed to be autofit before producing the PDF since the content of the row changed sizes based on a reference to another sheet. If you want something like that, don't forget to you'll need to add an activate command in the Dynamic Range sub to make sure the page updates before PDF creation.

    Thanks again to everyone who helped. I hope someone finds this file useful and can adapt it to make some repetitive task easier for them.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1