472,353 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Columns and Rows in Excel

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

Dec 19 '05 #1
5 10609
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?


Is the worksheet currently open in Excel or OpenOffice,
and you wish to query the application for that information?

Or do you want to read the spreadsheet in from a file?

--
Steven.

Dec 19 '05 #2
In Python you probaly best follow a recipe like this.

1 Save the file in Excel as a csv file.
2.Study the reader object of Python's csv module :
http://docs.python.org/lib/module-csv.html
3. Read a row for row in in a list, split it on comma, count the
elements, the maximum of all these is the number of columns you look
for.
4. The number of rows you read is the number of rows in the sheet.

Dec 19 '05 #3
Greetings,

The worksheet is currently opened in Excel.
And I want to read the data from the excel worksheet.
Instead of looping through the entire worksheet, I want to limit the looping
to the rows and columns used so far!

Thanks and regards,
Anand

"Steven D'Aprano" <st***@REMOVEMEcyber.com.au> wrote in message
news:43************@REMOVEMEcyber.com.au...
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?


Is the worksheet currently open in Excel or OpenOffice,
and you wish to query the application for that information?

Or do you want to read the spreadsheet in from a file?

--
Steven.

Dec 19 '05 #4
> "Steven D'Aprano" <st***@REMOVEMEcyber.com.au> wrote in message
news:43************@REMOVEMEcyber.com.au...
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?
Is the worksheet currently open in Excel or OpenOffice,
and you wish to query the application for that information?

Or do you want to read the spreadsheet in from a file?

Anand wrote [at the top of the post, which was naughty]: Greetings,

The worksheet is currently opened in Excel.
And I want to read the data from the excel worksheet.
Instead of looping through the entire worksheet, I want to limit the looping to the rows and columns used so far!


An observation: if I record a macro and then use CTRL/End to go to the
"bottom right-hand corner" of the spread, I see that Excel records:

ActiveCell.SpecialCells(xlLastCell).Select

It's then just a matter of finding out the row and column number of the
currently-selected cell (or of xlLastCell if you don't want to change
locations).

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Dec 19 '05 #5
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).

Dec 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: RR | last post by:
Normally when I run a query, each record is displayed in a row, and the colums contain the field titles. Is there a way to switch this and have...
7
by: deep022in | last post by:
guys, I have written a perl script using Win32::OLe module. I was able to read it completly. but I have to specify the number of rows in excel...
0
by: sysmanint1 | last post by:
I am a total neophyte at Visual Basic but found the following post and reply from Clint concerning a dynamic range. Also, have never "posted" to a...
1
by: =?Utf-8?B?RGVicmEgTGFzc21hbg==?= | last post by:
I have installed Office 2007 on my machine and when I open Excel 2007 I still have 256 columns and 65,536 rows. I know in the 2007 version that you...
0
by: Radu | last post by:
Hi. I'm trying to read the structure of some Excel 2000 file that the user is expected to upload. The file must be an excel file (solved) and it...
0
by: JFKJr | last post by:
I have an excel file, which has columns C and D grouped together, I am trying to delete blank columns and rows from the excel file, ungroup the...
1
by: brionesl | last post by:
Hi, I have seen other threads with the same question but I'm not too familiar with Access and their solutions often include writing VB code or very...
5
by: jenniferhelen | last post by:
I have been searching threads for a while and found the instructions listed below many times, however when I get to step 6 and select to save, I...
0
by: ebernon | last post by:
The Dynamic deletion of Rows and Columns within a program is frequently desired but often hard to obtain. The Help files for VB-6 contained within...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.