+ Reply to Thread
Results 1 to 8 of 8

returning the address of a named range in VBA

Hybrid View

zinny returning the address of a... 09-19-2008, 09:26 PM
protonLeah How about: dim x as... 09-19-2008, 09:59 PM
zinny that code works great if i... 09-20-2008, 02:39 AM
Leith Ross Hello Zinny, To make your... 09-20-2008, 02:47 AM
protonLeah That's significantly... 09-20-2008, 08:05 PM
zinny thanks. i'll have a look into... 09-21-2008, 12:33 AM
mikerickson MsgBox... 09-20-2008, 11:56 PM
MatrixMan Physical or named address 09-22-2008, 05:18 AM
  1. #1
    Registered User
    Join Date
    09-13-2008
    Location
    Australia
    Posts
    12

    returning the address of a named range in VBA

    i'm trying to return the address of a named range.
    ie: if i have a named range called "names" on worksheet "main"

    C

    4 Adam
    5 Ben
    6 Michael

    I'de like to return "C4:C6"

    i can do this on the sheet ADDRESS(ROW(names),COLUMN(names)) but VBA doesn't recognise these commands.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,935
    How about:

    dim x as string
    x=ActiveWorkbook.Names("names")
    ,
    or
    x = Range("bluenames").Address
    Last edited by protonLeah; 09-19-2008 at 10:03 PM.
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    09-13-2008
    Location
    Australia
    Posts
    12
    that code works great if i run it on the current worksheet but i'm running it from a different excel file. What i am trying to accomplish is to copy a range of cells from a closed file into the current worksheet. All my variables are correct because i've debug.printed them. its only the line:
    first_range = Workbooks("FileNamesList(i)").ActivateRange(TOTAL_SUPPLIER_CHARGE).Address

    that gives me a run-time error 9. Subscripts out of range.
    TOTAL_SUPPLIER_CHARGE refers to cell C34 and TOTAL_PROFIT refers to cell E34.
    these cell values may change if the user inserts a new line therefore the name lists have to be used.

    the variable FileNamesList(i) prints the correct file name ie: test1.xls
    first_range and second_range are strings

    For i = 1 To UBound(FileNamesList) 
            first_range = Workbooks("FileNamesList(i)").ActivateRange(TOTAL_SUPPLIER_CHARGE & ":" & TOTAL_PROFIT).Address
    second_range = Workbooks("FileNamesList(i)").ActivateRange(TOTAL_PROFIT).address        
    GetValuesFromAClosedWorkbook UserDirectory, FileNamesList(i), "Main", first_range & ":" & second_range, i
            Cells(i + 4, 2).Formula = FileNamesList(i)
    Next i

    Sub GetValuesFromAClosedWorkbook(fPath As String, _
                fName As Variant, sName, cellRange As String, counter As Long)
    Dim rownumber As Long
    
        Debug.Print fPath, fName, sName, cellRange, counter
        rownumber = 4 + counter
     
        
            With ActiveSheet.Range("C" & rownumber & ":E" & rownumber)
              .FormulaArray = "='" & fPath & "\[" & fName & "]" _
                         & sName & "'!" & cellRange _
              .Value = .Value
            End With
      
    End Sub
    Last edited by zinny; 09-20-2008 at 02:59 AM.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Zinny,

    To make your posts easier to read and preserve your formatting, please wrap your code. I did it for you this time. Here is how you can do it next time.

    How to wrap your Code
    On the Message window Toolbar you will see the # icon. This will automatically wrap the text you selected with the proper HTML tags to create a Code Window in your post. You can do this manually by placing the tag [code] at the start of the line, and the tag [/code] at the end.

    As a new member please take so time to familiarize yourself with the Do's and Don'ts here in the Forum, just click on the link below...

    Forum Rules

    Sincerely,
    Leith Ross

  5. #5
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,935
    Quote Originally Posted by zinny View Post
    that code works great if i run it on the current worksheet but i'm running it from a different excel file. What i am trying to accomplish is to copy a range of cells from a closed file...
    That's significantly different from the OP. Here's some links regarding closed files/workbooks:
    Quote Originally Posted by davesexcel View Post

  6. #6
    Registered User
    Join Date
    09-13-2008
    Location
    Australia
    Posts
    12
    thanks. i'll have a look into those examples.
    i guess more specifically my program works if cellRange is a string "c34:e34"
    however i need to refer to a named lists "SUPPLIER_TOTAL_CHARGE" and "TOTAL_PROFIT" which is in the closed worksheet and refers to cell c34 and E34 respectively.
    Is there any way to set the value of cellRange so that it refers to a named list instead of entering the range manually?
    something like cellRange = Range("TOTAL_SUPPLIER_CHARGE").Address & ":" & Range("TOTAL_PROFIT").Address

    Sub GetValuesFromAClosedWorkbook(fPath As String, _
                fName As Variant, sName, cellRange As String, counter As Long)
    Dim rownumber As Long
    
        Debug.Print fPath, fName, sName, cellRange, counter
        rownumber = 4 + counter
     
        
            With ActiveSheet.Range("C" & rownumber & ":E" & rownumber)
              .FormulaArray = "='" & fPath & "\[" & fName & "]" _
                         & sName & "'!" & cellRange _
              .Value = .Value
            End With
      
    End Sub

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    MsgBox Workbooks("Workbook2.xls").Names("aNamedRange").RefersToRange.Address
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  8. #8
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    Physical or named address

    Hi Zinny; it looks like you're decomposing a named range into it's physical address and then using that to refer to the combined range ... which sort of defeats the purpose of a named range.

    It also looks like you're trying to use the physical address to write a formula (possibly TOTAL_SUPPLIER_CHARGE minus TOTAL_PROFIT?) .. Do you need the formula in the cell or just the result? If the latter, the simplest way would be:

    lngSupplierCosts = range("TOTAL_SUPPLIER_CHARGE").value - range("TOTAL_PROFIT").value
    If I've missed the point of what you're trying to do, perhaps a more specific explanation would help?

    MM

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Define variable range as named range
    By nigelog in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-30-2008, 09:28 AM
  2. Data Validation & Named Range
    By Dylan&Hayden in forum Excel General
    Replies: 5
    Last Post: 05-10-2008, 05:23 PM
  3. Named range of cells has disappeared!
    By Glio in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-23-2007, 12:03 PM
  4. value in cell to determine which named range used in formula
    By trashnation in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-31-2007, 04:20 AM
  5. Replies: 0
    Last Post: 09-11-2006, 10:40 PM

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