+ Reply to Thread
Results 1 to 11 of 11

Naming a Range of Cells where Range is Variable.

  1. #1
    Forum Contributor
    Join Date
    07-28-2008
    Location
    Leamington Spa, UK
    MS-Off Ver
    2010
    Posts
    142

    Question Naming a Range of Cells where Range is Variable.

    Hi All,

    I am having trouble trying to use VBA to add a Name to a range of cells. here is my code.
    Please Login or Register  to view this content.
    The code above runs fine, but it fails to name the range. Its something wrong in the 'ActiveWorkbook...' line. Can anyone offer me a fix if indeed it is this line?

    Regards,
    Mark
    Last edited by R_S_6; 08-10-2010 at 07:55 AM.

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Naming a Range of Cells where Range is Variable.

    You've got strcell and encell in quotes.

  3. #3
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Naming a Range of Cells where Range is Variable.

    Oh, and encell should be EndCell

  4. #4
    Forum Contributor
    Join Date
    07-28-2008
    Location
    Leamington Spa, UK
    MS-Off Ver
    2010
    Posts
    142

    Re: Naming a Range of Cells where Range is Variable.

    Hi Andrew.

    Thanks for that. Just removed the quotes - I get a 'Runtime Error 1004 - The formula typed contains an error...'

    Any Ideas?

    Cheers,
    Mark

  5. #5
    Forum Contributor
    Join Date
    07-28-2008
    Location
    Leamington Spa, UK
    MS-Off Ver
    2010
    Posts
    142

    Re: Naming a Range of Cells where Range is Variable.

    Current Code for that Line:

    Please Login or Register  to view this content.
    Still throws up same error using StartCell & EndCell

    Cheers

  6. #6
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Naming a Range of Cells where Range is Variable.

    Should it be strcell rather than StartCell?

  7. #7
    Forum Contributor
    Join Date
    07-28-2008
    Location
    Leamington Spa, UK
    MS-Off Ver
    2010
    Posts
    142

    Re: Naming a Range of Cells where Range is Variable.

    Theoretically encell=EndCell & strcell=StartCell. I'v etried the combination you suggest but still no luck :-(

  8. #8
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Naming a Range of Cells where Range is Variable.

    Try changing RefersToR1C1 to just RefersTo ... you're not providing the range in an R1C1 format.

  9. #9
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Naming a Range of Cells where Range is Variable.

    PS StartCell and strcell are not the same thing ... strcell is a string variable, but StartCell is an object, they're treated very differently.

  10. #10
    Forum Contributor
    Join Date
    07-28-2008
    Location
    Leamington Spa, UK
    MS-Off Ver
    2010
    Posts
    142

    Smile Re: Naming a Range of Cells where Range is Variable.

    Bingo - Got it working.

    Please Login or Register  to view this content.
    Used strcell & encell as these I believe (?) refer to the reference of the cell as opposed to the value in the cell (StartCell & EndCell). Removed the R1C1 as you suggested and its working as desired.

    Thanks very much for your help!

    Mark

  11. #11
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Naming a Range of Cells where Range is Variable.

    strcell and encell hold a string which is the cell's address, e.g. "B2", "D73"

    StartCell and EndCell are actually cell objects, so StartCell.Address will be the same as strcell, but you could also ask for StartCell.Row or StartCell.Formula, etc.

+ 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