+ Reply to Thread
Results 1 to 7 of 7

Problem getting name of range with only one cell

  1. #1
    Registered User
    Join Date
    03-16-2012
    Location
    Pune, India
    MS-Off Ver
    Excel 2007
    Posts
    5

    Problem getting name of range with only one cell

    Hi all,

    I'm facing a pretty peculiar problem in both, excel 2007 and 2010. I have created a vertical range(say, 'TestRng'- $A$1:$A$5). Now I delete last 4 rows, i.e. 2 through 5. 'TestRng' now refers to $A1:$A1(when I see it in name manager). Now if I select this cell and try to fetch its name using vba code:
    selection.name.name

    I get error. (Runtime error 1004. Application defined or object defined error)

    However, if I create a named range with just one cell(say, 'TestRng1' - $B$1), in the name manager it shows as referring to $B$1, and not $B$1:$B$1(as in the former case). Now if I select this cell and try to fetch its name using vba code:
    selection.name.name
    It works, i.e. I get correct name-'TestRng1'

    Can you help me get the correct name in the former case(where last n-1 rows are deleted)?

  2. #2
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394

    Re: Problem getting name of range with only one cell

    This is not the way to do this. You should convert your range to a "table". Use the menu item "insert\table". Tables are designed to solve this issue (among other things)

  3. #3
    Registered User
    Join Date
    03-16-2012
    Location
    Pune, India
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Problem getting name of range with only one cell

    Thanks Mallycat,
    however, I'm working on an existing template and need a way out using ranges. Cannot replace all required ranges with tables.

  4. #4
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,577

    Re: Problem getting name of range with only one cell

    Pl see the attached file with macro.
    macro defines name for the range which contains data from A1 to end of column A.
    k returns the name of range.

    Any clarifications welcome.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    03-16-2012
    Location
    Pune, India
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Problem getting name of range with only one cell

    Thanks kvsrinivasamurthy, really appreciate your help. However, the problem still persists.

    In the sheet u sent, if u ran your macro, it generated a range named 'erange1' - $A$1:$A$5. You can try this out on the same sheet:
    Delete last 4 rows, i.e. rows 2 to 5, such that range 'erange1' now references only one cell i.e. $A$1. Now if I select this cell and try to get it's name using:
    selection.name.name
    It gives error that I mentioned in the first post of this thread.

    Any help would be appreciated.

  6. #6
    Registered User
    Join Date
    03-16-2012
    Location
    Pune, India
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Problem getting name of range with only one cell

    Thanks a lot Rory.

    I was aware of this. But there are 400-500 names in the template and this part itself is inside a loop that runs about as many times. Not sure if there is a more efficient solution to this problem.

  7. #7
    Registered User
    Join Date
    03-16-2012
    Location
    Pune, India
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Problem getting name of range with only one cell

    Thanks Rory,

    That is exactly how I have implemented, using exit for. May be I will have to be content with this, for the time being.

    Regards,
    Rahul

+ 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