Results 1 to 14 of 14

Thread: Need Help on selecting using vba on excel

Hybrid View

  1. #1
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Need Help on selecting using vba on excel

    The inspiration for this came from a forum, but I've lost the reference. Thanks be to whoever posted the original code. I hope this helps, even if you just use the standard "UsedRange" function.

    MODIFIED 8/25/2005 9:47 AM edt
    Code:
    '========================================================================
    ' **********************************************************************
    '
    ' FUNCTION "ActualUsedRange"
    '
    'This function returns the ACTUAL 'UsedRange' of cells from "A1" to the
    'cell intersection of the last non-empty Row and the last non-empty Column.
    'If the Sheet is empty, the function returns "Nothing" as the range.
    'This function does NOT count any formatted but otherwise empty cells.
    'Cells with only non-printing characters appear blank, but are actually not.
    'This is a replacement for the buggy Excel 'UsedRange' function, which
    'occasionally returns a range bigger than the actual Used Range.  NOTE: This
    'could take a LONG time to run!
    '
    'The return of "Nothing" can be detected in the calling code with the following:
    '    Dim myReturnedRange as Range
    '    Dim mySheet as Worksheet
    '    Set mySheet = ActiveSheet
    '    Set myReturnedRange = ActualUsedRange(mySheet)
    '    If myReturnedRange Is Nothing Then ...
    '
    ' **********************************************************************
    Function ActualUsedRange(anySht As Worksheet) As Range
    
    Dim i As Long      ' Loop Index
    Dim c As Integer   ' Column number
    Dim r As Long      ' Row number
    
    'Use Excel 'UsedRange' as an inclusive estimate
    With anySht.UsedRange
      'Find the total number of COLUMNS
      i = .Cells(.Cells.Count).Column
      if i < 256 then i = i + 1
    'Starting from the RIGHT, eliminate empty columns
      For c = i To 1 Step -1
        If Application.CountA(anySht.Columns(c)) > 0 Then Exit For
      Next c
      'Find the total number of ROWS
      i = .Cells(.Cells.Count).Row
      if i < 65536 then i = i + 1
    'Starting from the BOTTOM, eliminate empty rows
      For r = i To 1 Step -1
        If Application.CountA(anySht.Rows(r)) > 0 Then Exit For
      Next r
    End With
    
    'Set the return value with the ACTUAL range from $A$1 to the REAL 'Last Cell'
    If (r = 0 And c = 0) Then
      'Return 'Nothing' for an empty sheet
      Set ActualUsedRange = Nothing
    Else
      'Return the Actual Used Range determined for this sheet
      Set ActualUsedRange = anySht.Range(anySht.Cells(1, 1), anySht.Cells(r, c))
    End If
      
    End Function
    '
    Last edited by Webtest; Aug 25th, 2005 at 08:50 AM. Reason: Overflow Errors in Code
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width