UDF to return full address include sheet name

tony567

Well-known Member
Joined
Aug 23, 2008
Messages
515
Code:
Function MyUDF(r As Range)
    MsgBox r.Address
End Function


=MyUDF('Sheet3'!E3) the result is: msgbox $E$3

need to return full address include sheetname
what is the vba code to do that,


thank you
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Use the ExternalReference argument of the .Address property.

Code:
MsgBox ActiveCell.Address(True,False,xlA1,True)
 
Upvote 0
aha! thank you Mike, i think i understand what u mean...
now, how if we want to exclude the workbook name?
or is it always good to include the workbook name? i'll used it to referencing range in my UDF


Use the ExternalReference argument of the .Address property.

Code:
MsgBox ActiveCell.Address(True,False,xlA1,True)
 
Upvote 0
Code:
Function MyUDF(rng As Range) As String
    Application.Volatile
    If UBound(Split(rng.Parent.Name, " ")) > 0 Then
        MyUDF = "'" & rng.Parent.Name & "'!" & rng.Address(False, False, xlA1)
    Else
        MyUDF = rng.Parent.Name & "!" & rng.Address(False, False, xlA1)
    End If
End Function
 
Upvote 0
If you already have a range object, why do you want to use its address to refer to it in a udf?
 
Upvote 0
thank you rorya, anyway would please give me an example,
i just confuse when i try to put range in my UDF which is located in another worksheet.


If you already have a range object, why do you want to use its address to refer to it in a udf?
 
Upvote 0
just simple example UDF which give unexpected result when the range (r) located in another worksheet,
but the result is OK when the range (r) located on the same worksheet.

Code:
Function MyUDF(r As Range)
    MsgBox Evaluate("Sum(" & r.Address & ")")
End Function

If you already have a range object, why do you want to use its address to refer to it in a udf?
 
Upvote 0
Code:
Function MyUDF(r As Range)
    MsgBox r.worksheet.Evaluate("Sum(" & r.Address & ")")
End Function

For example.
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,854
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top