PC Review


Reply
Thread Tools Rate Thread

Can you use UsedRange.SpeciaCells() to loop through all Cells?

 
 
ExcelMonkey
Guest
Posts: n/a
 
      15th Jun 2008
I know I can use the .UsedRange property to loop through cells. I also know
I can use the UsedRange.SpecialCells() property to further define this. Is
it possible to use the SpecialCells() property and have it run as if you only
wanted the UsedRange by itself.

This loops through cells with formulas.
Thisworkbook.Worksheets("Sheet1").UsedRange.SpecialCells(xlformulas)

I want to continue to use this script but have the variable in brackets
after .SpecialCells mean all cells.
Thisworkbook.Worksheets("Sheet1").UsedRange.SpecialCells(something else)

I want to do this to avoid using two separate loops. I want one loop, and
then I want to pass a variable to the brackets after SpecialCells.

Make sense?

EM
 
Reply With Quote
 
 
 
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      15th Jun 2008
Can you use xlCellTypeVisible maybe?

Rick


"ExcelMonkey" <(E-Mail Removed)> wrote in message
news:F6FD373A-46FE-45E1-A0C5-(E-Mail Removed)...
>I know I can use the .UsedRange property to loop through cells. I also
>know
> I can use the UsedRange.SpecialCells() property to further define this.
> Is
> it possible to use the SpecialCells() property and have it run as if you
> only
> wanted the UsedRange by itself.
>
> This loops through cells with formulas.
> Thisworkbook.Worksheets("Sheet1").UsedRange.SpecialCells(xlformulas)
>
> I want to continue to use this script but have the variable in brackets
> after .SpecialCells mean all cells.
> Thisworkbook.Worksheets("Sheet1").UsedRange.SpecialCells(something else)
>
> I want to do this to avoid using two separate loops. I want one loop, and
> then I want to pass a variable to the brackets after SpecialCells.
>
> Make sense?
>
> EM


 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      15th Jun 2008
Maybe you could build a range using .specialcells.

For instance, if I wanted to look at a range, but only wanted to loop through
the cells that were numeric and I didn't care if those numbers were the results
of formulas or just numeric constants, I could use:

Dim myNumConst As Range
Dim myNumFormulas As Range
Dim myNumCells As Range
Dim SearchRange as Range
dim rCell as range

Set SearchRange = activesheet.range("a1:x99")

Set myNumCells = Nothing
Set myNumConst = Nothing
Set myNumFormulas = Nothing

On Error Resume Next
Set myNumConst _
= SearchRange.Cells.SpecialCells(xlCellTypeConstants, xlNumbers)
Set myNumFormulas _
= SearchRange.Cells.SpecialCells(xlCellTypeFormulas, xlNumbers)
On Error GoTo 0

If myNumConst Is Nothing Then
Set myNumCells = myNumFormulas
Else
If myNumFormulas Is Nothing Then
Set myNumCells = myNumConst
Else
Set myNumCells = Union(myNumConst, myNumFormulas)
End If
End If

If myNumCells Is Nothing Then
'do nothing
Else
'do the work against the smaller range
For Each rCell In myNumCells.Cells
'...
next rCell
End If



ExcelMonkey wrote:
>
> I know I can use the .UsedRange property to loop through cells. I also know
> I can use the UsedRange.SpecialCells() property to further define this. Is
> it possible to use the SpecialCells() property and have it run as if you only
> wanted the UsedRange by itself.
>
> This loops through cells with formulas.
> Thisworkbook.Worksheets("Sheet1").UsedRange.SpecialCells(xlformulas)
>
> I want to continue to use this script but have the variable in brackets
> after .SpecialCells mean all cells.
> Thisworkbook.Worksheets("Sheet1").UsedRange.SpecialCells(something else)
>
> I want to do this to avoid using two separate loops. I want one loop, and
> then I want to pass a variable to the brackets after SpecialCells.
>
> Make sense?
>
> EM


--

Dave Peterson
 
Reply With Quote
 
ExcelMonkey
Guest
Posts: n/a
 
      16th Jun 2008
Interesting. May give this a whirl.

Thanks

EM

"Dave Peterson" wrote:

> Maybe you could build a range using .specialcells.
>
> For instance, if I wanted to look at a range, but only wanted to loop through
> the cells that were numeric and I didn't care if those numbers were the results
> of formulas or just numeric constants, I could use:
>
> Dim myNumConst As Range
> Dim myNumFormulas As Range
> Dim myNumCells As Range
> Dim SearchRange as Range
> dim rCell as range
>
> Set SearchRange = activesheet.range("a1:x99")
>
> Set myNumCells = Nothing
> Set myNumConst = Nothing
> Set myNumFormulas = Nothing
>
> On Error Resume Next
> Set myNumConst _
> = SearchRange.Cells.SpecialCells(xlCellTypeConstants, xlNumbers)
> Set myNumFormulas _
> = SearchRange.Cells.SpecialCells(xlCellTypeFormulas, xlNumbers)
> On Error GoTo 0
>
> If myNumConst Is Nothing Then
> Set myNumCells = myNumFormulas
> Else
> If myNumFormulas Is Nothing Then
> Set myNumCells = myNumConst
> Else
> Set myNumCells = Union(myNumConst, myNumFormulas)
> End If
> End If
>
> If myNumCells Is Nothing Then
> 'do nothing
> Else
> 'do the work against the smaller range
> For Each rCell In myNumCells.Cells
> '...
> next rCell
> End If
>
>
>
> ExcelMonkey wrote:
> >
> > I know I can use the .UsedRange property to loop through cells. I also know
> > I can use the UsedRange.SpecialCells() property to further define this. Is
> > it possible to use the SpecialCells() property and have it run as if you only
> > wanted the UsedRange by itself.
> >
> > This loops through cells with formulas.
> > Thisworkbook.Worksheets("Sheet1").UsedRange.SpecialCells(xlformulas)
> >
> > I want to continue to use this script but have the variable in brackets
> > after .SpecialCells mean all cells.
> > Thisworkbook.Worksheets("Sheet1").UsedRange.SpecialCells(something else)
> >
> > I want to do this to avoid using two separate loops. I want one loop, and
> > then I want to pass a variable to the brackets after SpecialCells.
> >
> > Make sense?
> >
> > EM

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Clear UsedRange except specific cells? Ray Microsoft Excel Programming 6 7th Aug 2007 12:26 PM
Help - loop through cells in a range that are not together (several different cells as Target) Marie J-son Microsoft Excel Programming 4 3rd Apr 2005 09:54 PM
Loop through cells Sheeny Microsoft Excel Programming 7 6th May 2004 12:02 AM
UsedRange & Formatting of Cells JStone0218 Microsoft Excel Programming 3 26th Oct 2003 11:36 PM
Using UsedRange as limits in a For Each loop but for cells on another sheet ? tur13o Microsoft Excel Programming 2 23rd Oct 2003 01:18 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:38 AM.