LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Looping in Excel XP with C# -- Ranges and SpecialCells

I'm writing to code that needs to check ALL text in a Workbook. The
initial solution - looping through the 'UsedRange' - works fine:

range = sheet.UsedRange;
for (int row = 1; row <= range.Rows.Count; row++) {
for (int col = 1; col <= range.Columns.Count; col++){
cell = (Excel.Range)range.Cells[row, col];
// do stuff with cell.Value2
}
}

BUT then when I tried it with a *complex* XLS (9 sheets, lots of
cells) it takes *forever* (almost 90 minutes) to process.

So I started investigating the 'SpecialCells' property which *should*
return a range with only 'matching' cells and not blanks.

Excel.Range newrange =
range.SpecialCells(Microsoft.Office.Interop.Excel. XlCellType.xlCellTypeConstants,
(object)3 );

To give you some idea - on Sheet 1 of my XLS, UsedRange.Count = 22,000
but SpecialCells returned newrange.Count = 192 (the data is quite
spread out). I'm thinking that will *significantly* reduce my
processing time...

HOWEVER, the range returned by SpecialCells has Rows=1 and Columns=1
(even though Count=192) which means I cannot iterate through it using
the loop shown above. I also can't iterate through it using FOREACH
because of http://support.microsoft.com/?kbid=328347
It looks like the range returned by SpecialCells is a combination of
the first matching cell, with the Cells collection containing the rest
- but I can't iterate through them!
The get_Item() method requires row & column parameters, but I can't
determine them either.

Any suggestions MUCH appreciated.


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
LOOPING STRUCTURE DESIRED 4 TWO RANGES FARAZ QURESHI Excel Discussion (Misc queries) 2 June 5th 09 05:24 PM
LOOPING multiple ranges Jase Excel Discussion (Misc queries) 1 April 7th 08 06:21 PM
Looping in VB with cell ranges Freeman Excel Worksheet Functions 2 January 22nd 06 12:14 PM
Looping through Ranges of Rows MS News Excel Programming 6 December 9th 03 06:13 AM
Looping through Ranges of Rows MS News Excel Programming 0 December 8th 03 11:42 AM


All times are GMT +1. The time now is 07:14 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"