You are here:

Excel/Displaying the last row number of each sheet

Advertisement


Question
Hi dave,
There are  10 worksheets in a work book.I want each sheet name and the last row number of respective sheet to be displayed in a new work book.Is it possible?How to write macro for it?your help is greatly appreciated.

regards,

Baskar

Answer
Hi,

Give this a go,

Sub LoopThroughSheets()

' LoopThroughSheets Macro
'-1st adds a workbook
'-Loops though sheets gets last row from each sheet

   Dim shts As Worksheet, Wb As Workbook, WBK As Workbook, WbSh As
Worksheet
   Set WBK = ThisWorkbook
   Dim Rws As Long, Col As Integer, r As Range, fRng As Range, LstRw As
Long

   Set Wb = Workbooks.Add    'adds a workbook
   Set WbSh = Wb.Worksheets(1)    'Sets Sheet 1 in new workbook

   For Each shts In WBK.Sheets    'Loops through sheets
       With shts
         Set r = Range("A1")

         Rws = .Cells.Find(What:="*", After:=r, SearchOrder:=xlByRows,
SearchDirection:=xlPrevious).Row
         Col = .Cells.Find(What:="*", After:=r, SearchOrder:=xlByColumns,
SearchDirection:=xlPrevious).Column
         Set fRng = Cells(Rws, Col)    ' range A2 to last cell on sheet
         LstRw = WbSh.Cells(Rows.Count, "A").End(xlUp).Row + 1

         WbSh.Cells(LstRw, "A") = shts.Name
         WbSh.Cells(LstRw, "B") = fRng.Row


       End With
   Next shts

End Sub
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Dave Morrison

Expertise

I can answer most excel questions My Website is davesexcel.com

Experience

I have been working with spreadsheets since 1991. I am a self-trained excel developer since 2005.

Publications
My website davesexcel.com

Education/Credentials
Business admin. U of R.

Awards and Honors
Microsoft Excel MVP 2011-2012

©2016 About.com. All rights reserved.