INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

VBA Visual Basic for Applications (Microsoft) FAQ

Excel How To

How can I identify UsedRange OR last/first column/row etc. by Loomah
Posted: 1 Jul 02 (Edited 24 Mar 07)

A common problem is that UsedRange isn't 100% reliable

The following code will find the first and last used cells and then select a contiguous range between them.

CODE

Sub FindUsedRange()
    Dim LastRow As Long
    Dim
FirstRow As Long
    Dim
LastCol As Integer
    Dim
FirstCol As Integer

    ' Find the FIRST real row
    FirstRow = ActiveSheet.Cells.Find(What:="*", _
      SearchDirection:=xlNext, _
      SearchOrder:=xlByRows).Row
      
    ' Find the FIRST real column
    FirstCol = ActiveSheet.Cells.Find(What:="*", _
      SearchDirection:=xlNext, _
      SearchOrder:=xlByColumns).Column
    
    ' Find the LAST real row
    LastRow = ActiveSheet.Cells.Find(What:="*", _
      SearchDirection:=xlPrevious, _
      SearchOrder:=xlByRows).Row

    ' Find the LAST real column
    LastCol = ActiveSheet.Cells.Find(What:="*", _
      SearchDirection:=xlPrevious, _
      SearchOrder:=xlByColumns).Column
        
'Select the ACTUAL Used Range as identified by the
'variables identified above

ActiveSheet.Range(Cells(FirstRow, FirstCol), _
    Cells(LastRow, LastCol)).Select
End Sub

It is possible to use the elements of this Sub in part and to adapt them.  For instance to find the first empty row to insert data use the LastRow element and add 1.
eg

CODE

Sub MIT()
Dim lRow As Long
    ' Find the FIRST EMPTY row by adding 1 to the last row
    lRow = ActiveSheet.Cells.Find(What:="*", _
      SearchDirection:=xlPrevious, _
      SearchOrder:=xlByRows).Row + 1
    
    'Paste the data into the first
    'COMPLETELY empty row

    ActiveSheet.Paste Destination:=Cells(lRow, 1)
End Sub

This is an adaptation of code by Rodney Powell (xl mvp) who in turn adapted it from Bob Umlas(again xl mvp).  The method utilised here (... .Find(What:="*", ...) I have found useful in many more instances, such as identifying non blank rows/columns etc.

Amended 8 Mar 07 following GlennUK's comments about my deliberate mistake in not initialising FirstCol whilst initialising FirstRow twice.  Well spotted Glenn, it must have only been like that for about 4 years!!
Then again on 24 March 07 as I'm quite obviously a complete muppet!
winky smile

Back to VBA Visual Basic for Applications (Microsoft) FAQ Index
Back to VBA Visual Basic for Applications (Microsoft) Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close