Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Range based on interior color index?

Is there a way for me to define a range based on a particular interior
color index value? For example, can I "select" a group of cells on a
sheet that have a color index of 38?

Thanks in advance for any help!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Range based on interior color index?

Hi Garle,

I think that you eould need to loop through the cells and bulid the required
range, e.g.:

'=============
Public Sub Tester3()
Dim Rng As Range
Dim Rng2 As Range
Dim rCell As Range

Set Rng = Range("A1:K100")

For Each rCell In Rng.Cells
If rCell.Interior.ColorIndex = 38 Then
If Rng2 Is Nothing Then
Set Rng2 = rCell
Else
Set Rng2 = Union(rCell, Rng2)
End If
End If
Next rCell

Rng2.Select

End Sub
'<<=============

---
Regards,
Norman


"garle" wrote in message
ups.com...
Is there a way for me to define a range based on a particular interior
color index value? For example, can I "select" a group of cells on a
sheet that have a color index of 38?

Thanks in advance for any help!



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Range based on interior color index?

Thank you, Norman, works great!

I tried converting the sub to a function that returns the range of
cells with a given interior color index value as follows:

Function findColorInRange(searchrange As range, colorix As Integer)
Dim resultrange As range
Dim rCell As range

For Each rCell In searchrange.Cells
If rCell.Interior.ColorIndex = colorix Then
If resultrange Is Nothing Then
Set resultrange = rCell
Else
Set resultrange = Union(rCell, resultrange)
End If
End If
Next rCell

findColorInRange = resultrange

End Function


However, I get an error (run-time error 91) when I try to call with:

Sub testing()
Dim rng As range

rng = findColorInRange(range("database"), 38)

End Sub

Any ideas? I have the feeling I'm missing something basic, but I don't
know much about calling routines in macros.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Range based on interior color index?

Hi garle, You have to state what you want to return from the function,
like:

Function findColorInRange(searchrange As range, colorix As Integer) As
Range

As you thought, it was just something basic! : )

//Anders
ExcelSpecialisten SWE

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Range based on interior color index?

Thank you, Anders.

I still got an error after this change (but it got thrown in the
function, not the sub). I fiddled around and eventually added 'set' to
the assignments in both the calling sub and the function and was able
to get it to work properly.

Can anyone explain why I need to use:

Set findColorInRange = resultrange // from function

and

Set rng = findColorInRange(range("database"), 38) // from calling sub

I guess I've been clear on when to use 'set" and when not to...



Reply
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
lock cells based on interior color MIke Excel Discussion (Misc queries) 4 December 27th 07 08:59 PM
Range: changing interior color Jim Brass Excel Programming 4 November 11th 05 04:03 AM
color the interior of a range Pierre via OfficeKB.com Excel Worksheet Functions 1 November 2nd 05 12:55 PM
Interior color based on dates Michael Wise[_23_] Excel Programming 5 June 30th 05 10:02 PM
Find range by interior color CG Rosén Excel Programming 3 December 10th 03 01:12 AM


All times are GMT +1. The time now is 12:47 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"