You are here:

Excel/Finding the last row of each sheet for a column

Advertisement


Question
Hi Dave,

Thanks for your code.It was really Awesome.The below code written by you finds the last used row in each sheet and pastes the row number with each sheet name in the new sheet.How can we modify it to find Last used row in each sheet for a specific column say column B.

Thanks a Lot

Code:-

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

Answer
This should work,


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
   Dim Rws As Long, r As Range, fRng As Range, LstRw As Long
   Set WBK = ThisWorkbook

   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

           Rws = shts.Cells(Rows.Count, "B").End(xlUp).Row
           LstRw = WbSh.Cells(Rows.Count, "A").End(xlUp).Row + 1

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

       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.