Tuesday, April 8, 2014

Excel, Knowing the last row per sheet

Here is a very simple automation trick on how to find out the last row on an Excel Sheet.  This also shows how to find out the number of sheets, how to transfer between those, plus knowing the last row of each sheet.

Enjoy!


#Define xlLastCell 11

Local lcFile, loExcel As Excel.Application, lnLastRow, lnloop
lcFile = Getfile("xls,xlsx")
If !Empty(m.lcFile)   && check if not cancelled
      loExcel = Createobject("excel.application")
      With loExcel
            .Workbooks.Open(m.lcFile)
            For lnloop = 1 To .sheets.Count
                  .Worksheets(m.lnloop).Select
                  .ActiveCell.SpecialCells(xlLastCell).Select
                  lnLastRow = .ActiveCell.Row
                  Messagebox('Sheet'+Transform(m.lnloop)+' last Row'+;
                                     Transform(m.lnLastRow))
            Next
            * Visually Check
            .Visible = .T.
      Endwith
Else
      Messagebox("Aborted by user!",0+64,"Oppppssss!")
Endif

No comments:

Post a Comment