Consulting

Results 1 to 7 of 7

Thread: Sleeper: Searching For the Best Statement to Define a Named Range

  1. #1

    Sleeper: Searching For the Best Statement to Define a Named Range

    When I had to define some ranges programatically (sp?), I came up with the following for the cell "L4":
    ActiveWorkbook.Names.Add Name:="RngNm1", RefersToR1C1:="=Main!R4C12"
    It occurred that this is a pretty tough statement to write if I wanted to define a name for a cell in column "HG". Who knows what the column number of column "HG" is? So I rewrote the statement like this:
    ActiveWorkbook.Names.Add Name:="RngNm2", RefersToR1C1:="=Main!R4C" & Range("HG1").Column
    I gotta admit I was surprised when it worked!
    If any of you folks have a slicker (and shorter) statement for the name-defining task, I'd love to see it.

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Well, if you're just trying to get away from the R1C1 reference style, you could always go with:

    ActiveWorkbook.Names.Add Name:="RngNm2", RefersTo:="=Main!HG1"
    Pretty much about as short as you can go though.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by kpuls
    Well, if you're just trying to get away from the R1C1 reference style, you could always go with:

    ActiveWorkbook.Names.Add Name:="RngNm2", RefersTo:="=Main!HG1"
    Pretty much about as short as you can go though.
    Is that a challenge?

    Worksheets("Main").Range("HG1").Name= "RngNm2"
    You should also note that by not using an absolute reference in the RefersTo value, the name will refer all over the place, the only place it will refer to HG1 is in A1.

    .
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    I just knew there was a better way to write that statement. Now I have to figure out which of the two formats I prefer.
    Quote Originally Posted by xld
    You should also note that by not using an absolute reference in the RefersTo value, the name will refer all over the place, the only place it will refer to HG1 is in A1.
    I'm not sure I follow your comments, xld, especially
    "the only place it will refer to HG1 is in A1". Could you elaborate a bit on that?

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Sid,
    HG1 is a relative reference
    If you run
    ActiveWorkbook.Names.Add Name:="RngNm2", RefersTo:="=Main!HG1"

    this will add the range name. If you press PF5 and enter RngNm2, the selected cell will be that offset 215 columns on the same row as the active cell.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    OK, here's what my testing showed up.
    1. xld's statement seems to be the format of choice.

    Worksheets("Main").Range("HG1").Name= "RngNm2"

    2. Ken's format works if you want to assign a name to a literal, in this case "Main!HG1". It does not assign a name to cell HG1. The name shows up in the names table, but if you put your cursor on the cell, the name isn't there. I put the name in a formula like:
    =RngNm2
    and got the string "Main!HG1" as the returned value. I had put the value 100 into the cell "HG1".

    ActiveWorkbook.Names.Add Name:="RngNm2", RefersTo:="=Main!HG1"

    3. Apparently if you want to use a "RefersTo.." parameter, then it's gotta be "RefersToR1C1". It seems that none of the others will work.

    So that's that, folks.

    Oh, and thanx for clarification, Malcolm.

  7. #7
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Quote Originally Posted by xld
    Is that a challenge?

    Worksheets("Main").Range("HG1").Name= "RngNm2"
    You should also note that by not using an absolute reference in the RefersTo value, the name will refer all over the place, the only place it will refer to HG1 is in A1.

    .
    Fine then, prove me wrong, why don't you!

    Thanks for the feedback, Bob. I should have mentioned the relativity too, you're right.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





Posting Permissions

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