Share via

Reference entire sheet in Excel formula

Ken Krugh 121 Reputation points
2023-03-14T13:20:59.1933333+00:00

Is there something I can put in a formula that is equivalent to .UsedRange in VBA?

I'd like to pass the entire used range as the parameter to a custom function without having to update the parameter each time the number of rows in the sheet grows or shrinks.

Thanks,
Ken

Microsoft 365 and Office | Development | Other
Microsoft 365 and Office | Excel | For business | Windows
0 comments No comments

1 answer

Sort by: Most helpful
  1. Viorel 126.9K Reputation points
    2023-03-14T17:18:15.08+00:00

    I think that ActiveSheet.UsedRange will give you the range. Therefore you can use a formula like =MyFunction() without parameter.

    The function is, for example:

    Public Function MyFunction() As String
    
        Application.Volatile True
    
        Dim ws As Worksheet
        Set ws = ActiveSheet
        
        Dim r As Range
        Set r = ws.UsedRange
        
        '. . . perform calculations and return the result . . .
    
        MyFunction = "the result"
    
    End Function
    

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.