How to identify last Row which contains datas

Nagoo

New Member
Joined
Jul 2, 2015
Messages
30
Hi Friends / Rick,

My excel sheet has say 29 columns. I need to enter certain formula in the 30th column.

I have 'n' numbers of rows with contents. I need to find the last row which carries the contents and insert the formula in 'n16' cell to the upper cell.

But the problem is, the last row may not contain datas in all the cells (i,e 15 columns) only in one or two cells it may be available.

Please help me with a macro to find the last row which has the contents. Or it would be even better if i get the cell reference (i.e "n30") in which I need to insert the formula in last row.

Thanks for the help :)

 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
See if this does what you want...

Code:
Sub FindUsedRange()
  Dim FirstCell As Range, LastCell As Range
  Set LastCell = Cells(Cells.Find(What:="*", SearchOrder:=xlRows, _
      SearchDirection:=xlPrevious, LookIn:=xlValues).Row, _
      Cells.Find(What:="*", SearchOrder:=xlByColumns, _
      SearchDirection:=xlPrevious, LookIn:=xlValues).Column)
  Set FirstCell = Cells(Cells.Find(What:="*", After:=LastCell, SearchOrder:=xlRows, _
      SearchDirection:=xlNext, LookIn:=xlValues).Row, _
      Cells.Find(What:="*", After:=LastCell, SearchOrder:=xlByColumns, _
      SearchDirection:=xlNext, LookIn:=xlValues).Column)
  MsgBox "The Address of the last cell is " & LastCell.Address
End Sub

HTH

igold
 
Upvote 0
See if this does what you want...

Code:
Sub FindUsedRange()
  Dim FirstCell As Range, LastCell As Range
  Set LastCell = Cells(Cells.Find(What:="*", SearchOrder:=xlRows, _
      SearchDirection:=xlPrevious, LookIn:=xlValues).Row, _
      Cells.Find(What:="*", SearchOrder:=xlByColumns, _
      SearchDirection:=xlPrevious, LookIn:=xlValues).Column)
  Set FirstCell = Cells(Cells.Find(What:="*", After:=LastCell, SearchOrder:=xlRows, _
      SearchDirection:=xlNext, LookIn:=xlValues).Row, _
      Cells.Find(What:="*", After:=LastCell, SearchOrder:=xlByColumns, _
      SearchDirection:=xlNext, LookIn:=xlValues).Column)
  MsgBox "The Address of the last cell is " & LastCell.Address
End Sub

HTH

igold
Your routine will fail to find a cell containing a formula that returns "" outside the "used range" that it currently finds. Better to change the xlValues to xlFormulas to catch this. If there are no such cells then making that change will still produce the result you get w/o the change.
 
Upvote 0
JoeMo,

Thanks for the tip. I appreciate the insight...

igold
 
Upvote 0
See if this does what you want...

Code:
Sub FindUsedRange()
  Dim FirstCell As Range, LastCell As Range
  Set LastCell = Cells(Cells.Find(What:="*", SearchOrder:=xlRows, _
      SearchDirection:=xlPrevious, LookIn:=xlValues).Row, _
      Cells.Find(What:="*", SearchOrder:=xlByColumns, _
      SearchDirection:=xlPrevious, LookIn:=xlValues).Column)
  Set FirstCell = Cells(Cells.Find(What:="*", After:=LastCell, SearchOrder:=xlRows, _
      SearchDirection:=xlNext, LookIn:=xlValues).Row, _
      Cells.Find(What:="*", After:=LastCell, SearchOrder:=xlByColumns, _
      SearchDirection:=xlNext, LookIn:=xlValues).Column)
  MsgBox "The Address of the last cell is " & LastCell.Address
End Sub

HTH

igold


Hi Igold,

How do I select the specific cell which the Msgbox pops up ?? I mean, to select the cell which is available in Range 'LastCell.Address'
 
Upvote 0
Well other than just taking your cursor and putting it in the LastCell... you could add this line to the end of the code. Either before, after or instead of the last line that starts with "MsgBox"

Code:
LastCell.Select

igold
 
Upvote 0
No problem, glad I could help. Thanks for the feedback!

igold
 
Upvote 0

Forum statistics

Threads
1,215,634
Messages
6,125,931
Members
449,274
Latest member
mrcsbenson

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