Results 1 to 3 of 3

Thread: [Resolved] Naming Cell, dynamically defined by string

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    2

    Resolved [Resolved] Naming Cell, dynamically defined by string

    Hi, I'm attempting to name a particular cell, based on a string.
    Code:
      Dim CellName As String
      Dim extendedName As String
      
    ' Somewhere CellName has been set, eg
      CellName = "H25"
    
      ActiveWorkbook.Names.Add Name:="Total", RefersToR1C1:= _
            "='Report 5'!" & CellName
    This gives me extra apostrophes around CellName. eg:
    Total ='Report 5'!'H25'

    So I tried defining the whole string first:
    Code:
      extendedName = "'Report 5'!" & CellName
      ActiveWorkbook.Names.Add Name:="Total", RefersToR1C1:= extendedName
    However, this then added quotation marks around the entire formula, eg:
    Total ="'Report 5'!H25"

    Does anyone know how to do this? It seems like I'm overlooking something really simple.
    Last edited by jkmyoung; Aug 16th, 2007 at 02:34 PM.

  2. #2
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: Naming Cell, dynamically defined by string

    Welcome to the forums!

    Try this:

    Code:
    cellname = "$H$25"
    ActiveWorkbook.Names.Add Name:="Total", RefersTo:="=Report!" & cellname
    The $'s are needed, otherwise the reference will be relative to cell position.
    Last edited by VBAhack; Aug 16th, 2007 at 10:58 AM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    2

    Re: Naming Cell, dynamically defined by string

    Ok. That's a good tip. It doesn't entirely solve the problem, there are still extra quotes or apostrophes either way.
    I think the problem may be more related to the fact that my sheet name needs to be surrounded in single quotes. Unfortunately, I am unable to rename this sheet.

    Update:
    ok removing the R1C1 at the end fixed it. Thanks.
    How does one resolve a thread?
    Last edited by jkmyoung; Aug 16th, 2007 at 02:33 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width