Columns and Rows in Excel

Gerard Flanagan grflanagan at yahoo.co.uk
Mon Dec 19 08:48:43 EST 2005


Anand wrote:

> Greetings,
>
> How can I find the number of active columns and rows used in an excel work
> sheet?
> What I mean is how can i find the last column and row or cell position,
> where the data is stored in an excel sheet?
>
> A code snippet would be of great help.
>
> Thanks for your cooperation and help.
>
> Best regards,
> Anand

Anand,

A worksheet has a UsedRange property that may be what you need.

Gerard

using Excel = Microsoft.Office.Interop.Excel;

private Excel.Workbook thisWorkbook = null;

protected void ThisWorkbook_Open()
{
	Excel.Worksheet sheet;
	Excel.Range usedrange;
	Excel.Range lastcell;

	sheet = (Excel.Worksheet) thisWorkbook.Sheets[1];
	usedrange = sheet.UsedRange;
	lastcell = sheet.Cells[ usedrange.Rows.Count, usedrange.Columns.Count
];
}


If you have access to the Spreadsheet in question you can name ranges
of cells:

    Insert -> Names -> Define

Then

	usedrange  = sheet.get_Range( "DATA", Type.Missing );

for example.  (Don't worry about Type.Missing).




More information about the Python-list mailing list