+ Reply to Thread
Results 1 to 7 of 7

Assign range name based on cell values

  1. #1
    Forum Contributor
    Join Date
    11-15-2008
    Location
    Arizona
    MS-Off Ver
    Excel 2013
    Posts
    179

    Assign range name based on cell values

    In my workbook sheet 11 has some ranges that need to have names based on cell values in sheet2 (for purposes of data validation lists).

    Range S28:S46 will assume the name of sheet2A11 & sheet2A3.
    (example name period_1unit_1)

    Range U28:U46 will assume the name of sheet2A11 & sheet2A4.

    Range W28:W46 will assume the name of sheet2A11 & sheet2A5

    etc.


    Right now I am calling the code when something is entered into A11.


    I have tried if statement and select case, but I ran into complications with both.

    I have posted both codes with the questions I have concerning those codes.

    Can you help me either use one of these codes or come up with a better way?


    SELECT CASE METHOD

    Please Login or Register  to view this content.
    Nothing happens with this code, and I know it is because I do not have a value assigned to A11, but I have no idea what the user will enter into A11. I just need all those named ranges to occur once something is entered into A11.

    IF STATEMENT METHOD

    Please Login or Register  to view this content.
    This code returns the desired result; however, I need to add to it, and I am not sure how. Something being entered into A11 results in 4 name codes. With an if statement I do not know how to display multiple results, which is why I tried the select case method
    Last edited by dsrt16; 01-25-2009 at 03:38 PM.

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    Doing something like that, you may have more than one name for the same range. I added a routine to delete it.

    Without seeing all of your code, I can only guess why you had problems. Try this routine. Modify the other ranges as I did for the first one.

    Obviously, the Change event is inserted into your sheet by right clicking the sheet tab, View Code and paste. The DeleteBook() can be in that section or a Module.
    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    11-15-2008
    Location
    Arizona
    MS-Off Ver
    Excel 2013
    Posts
    179
    Thanks for the reply. There are some parts of your code that I do not understand. I would appreciate some clarification on those points:

    1) What is the purpose of the line Range("A3").Value = "Test". To me that looks like you are setting the value of A3, and A3 is a cell where the teacher enters the value.

    2) Do I place this code in front of all the named ranges:

    Please Login or Register  to view this content.
    3) When I put the deletebookmark code in front of each range, does that say it will delete that if a new value is entered in A11 and then create the new name with the new value? Am I understanding that part correctly?

    Just for clarification, I am calling the naming from he entering in of A11-A18 since those are entered after A3-A6, but A3-A6 are also values that the teacher enters names into.

    Then the name range is the combination of a value in the A3-A6 range and the A11-A18 range.

  4. #4
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    The Change event fires everytime a change is made in any cell on that sheet. While we could have used Intersect() to only execute parts of the Change events code for a certain set of cells, Case Select is going to work for your needs. IF() would have worked as well. I prefer Case Select since it only executes for the first case that is True.

    Typically, one uses Intersect() and then exit the sub if the intesection range is Nothing.

    So, when A11 or A32 is changed, then your Case code executes.

    I set a value in A3 to "Test" just so the code that creates the Name would have a valid name. You should understand that building a Name must be valid. e.g. If A3 and A11 had values but they were deleted. The Change event fires and it tries to set the Name = "". Obviously, you can't do that.

    (2) Yes, put it before each line where you are adding a name. Otherwise, multiple changes would result in multiple Names that all referto the same range of cells.

  5. #5
    Forum Contributor
    Join Date
    11-15-2008
    Location
    Arizona
    MS-Off Ver
    Excel 2013
    Posts
    179
    Okay I am going to test this code,and let you know how it goes.

    Just FYI The cells will never be empty or deleted. Teachers can change the values entered in those cells, but they can not delete them, so it should be okay.

    Last question all of this is happening on the set-up worksheet which sets-up the entire program.

    Right now that page runs extremely slow. It takes forever upon entering a value in a cell before you can enter into another cell. I know it is not due to the millions of cells that are referenced by it, but by the VBA code. I took away the vba code, but left all the cell references, and it was very fast.

    On the set-up page I have code to hide rows based on cell values and then another naming code, and then this naming code.

    Is there a way to cause the page to run faster?

  6. #6
    Forum Contributor
    Join Date
    11-15-2008
    Location
    Arizona
    MS-Off Ver
    Excel 2013
    Posts
    179
    Yes, that code works beautifully!!!

    I love the addition of deleting the old named range. Great work!!

    I realized I should start a new thread about how slow the set-up page is running, and I will do so after I get the code all complete.

    Thanks for help with the name ranges coding!

  7. #7
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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