How do I get a range's address including the worksheet name, but not the workbook name, in Excel VBA?

前端 未结 11 1415
眼角桃花
眼角桃花 2020-12-25 10:34

If I have a Range object--for example, let\'s say it refers to cell A1 on a worksheet called Book1. So I know that calling Address()

相关标签:
11条回答
  • 2020-12-25 10:54

    [edit on 2009-04-21]

        As Micah pointed out, this only works when you have named that
        particular range (hence .Name anyone?) Yeah, oops!

    [/edit]

    A little late to the party, I know, but in case anyone else catches this in a google search (as I just did), you could also try the following:

    Dim cell as Range
    Dim address as String
    Set cell = Sheet1.Range("A1")
    address = cell.Name
    

    This should return the full address, something like "=Sheet1!$A$1".

    Assuming you don't want the equal sign, you can strip it off with a Replace function:

    address = Replace(address, "=", "")
    
    0 讨论(0)
  • 2020-12-25 10:55

    Why not just return the worksheet name with address = cell.Worksheet.Name then you can concatenate the address back on like this address = cell.Worksheet.Name & "!" & cell.Address

    0 讨论(0)
  • 2020-12-25 11:02
    Split(cell.address(External:=True), "]")(1)
    
    0 讨论(0)
  • 2020-12-25 11:03

    I found the following worked for me in a user defined function I created. I concatenated the cell range reference and worksheet name as a string and then used in an Evaluate statement (I was using Evaluate on Sumproduct).

    For example:

    Function SumRange(RangeName as range)   
    
    Dim strCellRef, strSheetName, strRngName As String
    
    strCellRef = RangeName.Address                 
    strSheetName = RangeName.Worksheet.Name & "!" 
    strRngName = strSheetName & strCellRef        
    

    Then refer to strRngName in the rest of your code.

    0 讨论(0)
  • 2020-12-25 11:04

    Only way I can think of is to concatenate the worksheet name with the cell reference, as follows:

    Dim cell As Range
    Dim cellAddress As String
    Set cell = ThisWorkbook.Worksheets(1).Cells(1, 1)
    cellAddress = cell.Parent.Name & "!" & cell.Address(External:=False)
    

    EDIT:

    Modify last line to :

    cellAddress = "'" & cell.Parent.Name & "'!" & cell.Address(External:=False) 
    

    if you want it to work even if there are spaces or other funny characters in the sheet name.

    0 讨论(0)
  • 2020-12-25 11:07

    For confused old me a range

    .Address(False, False, , True)

    seems to give in format TheSheet!B4:K9

    If it does not why the criteria .. avoid Str functons

    will probably only take less a millisecond and use 153 already used electrons

    about 0.3 Microsec

    RaAdd=mid(RaAdd,instr(raadd,"]") +1)

    or

    'about 1.7 microsec

    RaAdd= split(radd,"]")(1)

    0 讨论(0)
提交回复
热议问题