+ Reply to Thread
Results 1 to 4 of 4

Function Return Range

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-07-2004
    Posts
    314

    Function Return Range

    Hi all,

    Using Excel 2010.

    Trying to return a Range Object from a Function, but I receive "Object Required" from the calling Sub().

    I printed the Range Address from the Function that means the Range exists, at least in the Function.

    Thoughts on what I am doing wrong?

    thx
    w

    debug.print output
    rng $A$1:$N$7602
    GetExcelRangeFunc $A$1:$N$7602
    Option Explicit
    Sub Foo()
    Dim GetRange As Range
    Set GetRange = GetExcelRangeFunc(lngRows:=7, _
                                                     lngCols:=2)
    End Sub
    
    Public Function GetExcelRangeFunc(lngRows As Long, _
                                      lngCols As Long, _
                                      Optional lngSheetIndex As Long = 1) As Range
    
        'Declare objects
            Dim wb As Workbook
            Dim ws As Worksheet
            Dim rng As Range
            
        'Declare variables
            Dim strFile As String
            Dim lngMeRows As Long
            Dim lngMeCols As Long
    
        'Get file for import
            strFile = GetFDObjectName(strDialogType:="File", _
                                      strTitle:="Select A File For Import")
            
        'Open the import file
            Set wb = Workbooks.Open(strFile)
            Set ws = wb.Worksheets(lngSheetIndex)
            
        'Get last column - use row 7
            lngMeCols = GetLast(ws:=ws, _
                                RC:="c", _
                                lngRowColumn:=lngRows)
                                    
        'Get last row - use col 2
            lngMeRows = GetLast(ws:=ws, _
                                RC:="r", _
                                lngRowColumn:=lngCols)
                                    
        'Create range
            With ws
                Set rng = .Range(.Cells(1, 1), .Cells(lngMeRows, lngMeCols))
            End With
            
        'Pass object to function
            Set GetExcelRangeFunc = rng
            
            Debug.Print "rng", rng.Address
            Debug.Print "GetExcelRangeFunc", GetExcelRangeFunc.Address
            
        'Tidy up
            wb.Close
            Set rng = Nothing
            Set ws = Nothing
            Set wb = Nothing
       
    End Function
    Kind regards,
    w

    http://dataprose.org

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Function Return Range

    A range object refers to a range in an open workbook; your sub closes the workbook before exiting.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Contributor
    Join Date
    01-07-2004
    Posts
    314

    Re: Function Return Range

    Thanks Shg,

    Revised Function below.
    The lesson learned: keep Function really simple to a singular purpose.

    thx
    w

    Public Function GetExcelRangeFunc(ws As Worksheet, _
                                      lngRows As Long, _
                                      lngCols As Long) As Range
        'Declare objects
            Dim rng As Range
            
        'Declare variables
            Dim lngMeRows As Long
            Dim lngMeCols As Long
            
        'Get last column - use row 7
            lngMeCols = GetLast(ws:=ws, _
                                RC:="c", _
                                lngRowColumn:=lngRows)
                                    
        'Get last row - use col 2
            lngMeRows = GetLast(ws:=ws, _
                                RC:="r", _
                                lngRowColumn:=lngCols)
                                    
        'Create range
            With ws
                Set rng = .Range(.Cells(1, 1), .Cells(lngMeRows, lngMeCols))
            End With
            
        'Pass object to function
            Set GetExcelRangeFunc = rng
            
        'Tidy up
            Set rng = Nothing
       
    End Function

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Function Return Range

    Good job.

    Or, more compactly,

    Public Function GetExcelRangeFunc(wks As Worksheet, _
                                      iRow As Long, _
                                      iCol As Long) As Range
      Set GetExcelRangeFunc = wks.Range("A1").Resize(GetLast(wks, "r", iCol), _
                                                     GetLast(wks, "c", iRow))
    End Function
    
    Function GetLast(wks As Worksheet, sRowCol As String, iRowCol As Long) As Long
      With wks
        Select Case sRowCol
          Case "R", "r"
            GetLast = .Cells(.Rows.Count, iRowCol).End(xlUp).Row
          Case "C", "c"
            GetLast = .Cells(iRowCol, .Columns.Count).End(xlToLeft).Column
        End Select
      End With
    End Function
    Last edited by shg; 04-19-2015 at 07:44 PM.

+ 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. Return the Range From a Lookup Function
    By cmore in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-15-2014, 08:58 AM
  2. How to write a function to return a value within a given value range
    By Sonnerstudent in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-22-2013, 04:31 PM
  3. Function to return MAX value for a range
    By jwright650 in forum Excel General
    Replies: 8
    Last Post: 02-08-2011, 11:14 AM
  4. How to make an if/and function examine range and return
    By mkmed in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 02-06-2007, 05:45 PM
  5. custom function - return range
    By GinaW in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-19-2006, 07:15 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