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

前端 未结 11 1495
眼角桃花
眼角桃花 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 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.

提交回复
热议问题