Looping thru cells that actually have data

VenturSum

Board Regular
Joined
May 23, 2010
Messages
137
Hi,

Excel 2007 has a LOT of cells. The looping code (below) looks at all cells, and takes forever...

How can the loop be constrained to consider ONLY cells that have some data??
e.g.: text, formula, hyperlink, notes, comments, etc. AND ignore formatting

- Could .SpecialSells() be used?
- The backend Excel tables, only holds cells with data, could only those cells that have data be evaluated?

A test could be done inside the loop (IF the cell was null, goto the next cell) But this still takes processor time to evaluate.



Bad Code :(
Dim C as Range, ws as worksheet

For each c in ws.cells
' Do something
Next c
Any thoughts would be greatly appreciated.

John
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
How about

Code:
For Each C In ws.UsedRange
' Do something
Next C

and check for blanks within the loop.
 
Upvote 0
I have no idea where in the worksheet there is any data.
I could use some evaluation for last row and last cell.
but that still evaluates cells that have no data.

I'm wondering if it's possible to consider ONLY those cells that have data within this loop or any other looping syntax.

Thanks,
John
 
Upvote 0
UsedRange contains the cells with are used on a sheet. Also the cells between non-empty cells are part of it.

For instance: A1:Q30

or

E2:Q30

The range returned has a rectangular shape.
 
Upvote 0
To illustrate, in a blank sheet, enter some data, preferably not starting at A1. Then run

Code:
Sub ventur()
ActiveSheet.UsedRange.Select
End Sub
 
Upvote 0
WOW!

.UsedRange really speed things up.
But it also evaluates cells that have no data.
So I'm still wondering.. Is it possible to evaluate only cells that have data?

what about .SpecialCells()?

Any thoughts???
John
 
Upvote 0
What are you actually doing with the cells?
 
Upvote 0
Perhaps

Code:
Dim c As Range, r As Range
With ActiveSheet.UsedRange
    Set r = Union(.SpecialCells(xlCellTypeConstants), .SpecialCells(xlCellTypeFormulas))
End With
For Each c In r
'do something
Next c
 
Upvote 0
VOG - I'll give that a try, but I have to go now.

Norie - What am I doing?

I need to create a table that has all the text in the workbook.
The end product would be a couple columns

Code:
Address     Text
A1            Welcome
A2            Some text
D3            Some more Text
F7            = F6 + F5
FF34         Text not expected cause it's too far to scroll
So I need to evaluate all the cells in worksheet that actually have data.
and I don't know how big the worksheets are.

Thanks, John
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,045
Messages
6,122,830
Members
449,096
Latest member
Erald

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top