Saturday, February 8, 2014

Row Count and Column Count from Excel


Finding the last Row or Coumn:
Solution 1:
Process 1: (2/9/2014)

lastrow = Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row
MsgBox (lastrow) 'Total number of rows(which has data) in Excel.
lastcolumn = Cells.Find("*", [A1], , , xlByColumns, xlPrevious).Column
MsgBox(lastcolumn) ' gives total number of columns(which has data) in Excel.


4/7/15:

lastrow = Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row ' this gives the row number

' Lets suppose you applied filter, i found 2 records(row 2 and row 36) after applying filter, this code is giving me lastrow values as 36. so this formula cannot be used for counting rows.


5/7/15:
Solution 2:
'Get the Number of Rows used in the Excel sheetRowCount = ExcelObject.ActiveWorkbook.Sheets(1).UsedRange.Rows.count
msgbox "Number of rows used in the excel sheet "& RowCount

'Get the Number of Columns used in the Excel sheet

ColumnCount = ExcelObject.ActiveWorkbook.Sheets(1).UsedRange.Columns.count
msgbox "Number of columns used in the excel sheet "&ColumnCount

source: http://mahimavbscript.blogspot.com/2010/09/working-with-excel-object.html

Note: UsedRange sometimes gives disguising results, need to find out why.

Solution 3:

Selection method plays a major role here.


Ex:
        Workbooks(Filename).Sheets(Director).Range("B25").Select 'Dont know why not select
     
        Workbooks(Filename).Sheets(Director).Range(Selection, Selection.End(xlDown)).Select
        Workbooks(Filename).Sheets(Director).Range(Selection, Selection.End(xlToRight)).Select

No comments:

Post a Comment