Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / New Users / October 2006

Tip: Looking for answers? Try searching our database.

Coloring the Desired cells

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Harsh - 18 Oct 2006 19:49 GMT
Hello,

I have a work sheet in which i have to look for word "Test" and color
the rows below it.
There are different words like "Test 1"  "Test 2" and each set needs a
different color.
Can I get some help with the macro for it?

eg:
Test1
 row 1
 row 2

Test 2
 row 1
 row 2

the number of rows in each group is not constanr.

Thank you,
Harsh
Otto Moehrbach - 18 Oct 2006 23:12 GMT
Excel will need to know the logic of the rows and colors to be able to
determine how many rows to color.  You say the number of rows is not
constant, but obviously you know how many rows to color.  How do you know
that?  HTH  Otto

> Hello,
>
[quoted text clipped - 17 lines]
> Thank you,
> Harsh
Harsh - 18 Oct 2006 23:48 GMT
The spread sheet has many parameters. The Test 1 condition repeats and
the number of rows is uncertain . So the logic i was thinking search
Test 1 and color until Test 2 and so on...

> Excel will need to know the logic of the rows and colors to be able to
> determine how many rows to color.  You say the number of rows is not
[quoted text clipped - 22 lines]
> > Thank you,
> > Harsh
Otto Moehrbach - 19 Oct 2006 00:52 GMT
That sounds good.  The code could search for any entry that starts with
"Test" and go from there.  Do you have a list of what color goes with what
"Test X"?
Is all your data in one column?  What column?  If more than one, what
columns to search?  Answer these questions and I'll work you up something in
the morning (USA Easter time).   HTH   Otto
> The spread sheet has many parameters. The Test 1 condition repeats and
> the number of rows is uncertain . So the logic i was thinking search
[quoted text clipped - 26 lines]
>> > Thank you,
>> > Harsh
Harsh - 19 Oct 2006 01:58 GMT
I need to search Test in 2nd column. But the data below test is in
multiple columns.

eg.

Test1
Data1    data2     Data3    data4
Data1    data2     Data3    data4

Test2
Data1    data2     Data3    data4
Data1    data2     Data3    data4

and so on..

The number of columns too are unknown.

Thanks for your help,
Harsh....

> That sounds good.  The code could search for any entry that starts with
> "Test" and go from there.  Do you have a list of what color goes with what
[quoted text clipped - 32 lines]
> >> > Thank you,
> >> > Harsh
Otto Moehrbach - 19 Oct 2006 02:30 GMT
Is the number of columns constant for all the rows below any given Test X or
does that vary also?  Otto
>I need to search Test in 2nd column. But the data below test is in
> multiple columns.
[quoted text clipped - 57 lines]
>> >> > Thank you,
>> >> > Harsh
Harsh - 19 Oct 2006 04:36 GMT
Yes it varies too. and for each Test X the color should be different.

Test 1 red,
test 2 blue
test 3 green

and thee 3 test conditions repeat in the sheet. Both rows and columns
are not constant.
Thanks.

> Is the number of columns constant for all the rows below any given Test X or
> does that vary also?  Otto
[quoted text clipped - 59 lines]
> >> >> > Thank you,
> >> >> > Harsh
Otto Moehrbach - 19 Oct 2006 13:35 GMT
Harsh
   Try this out and see if it does what you want.  Paste all the following
code into a regular module.  Run this code when the sheet that has all the
Test data is the active sheet.  If you wish, send me an email and I'll send
you the small file I used to develop this code.  My email address is
ottokmnop@comcast.net.  Remove the "nop" from this address.  HTH  Otto

Option Explicit
Dim RngColB As Range, First As Range, Last As Range
Dim RngToColor As Range, i As Range, LastRow As Long
Dim c As Long, ColorNum As Long

Sub ColorTests()
   Call FindFirstTest
   Call ColorData
End Sub

Sub FindFirstTest()
   'Find first instance of "Test"
   Set RngColB = Range("B2", Range("B" & Rows.Count).End(xlUp))
   LastRow = RngColB(RngColB.Count).Row
   Set First = RngColB.Find(What:="Test", _
       After:=RngColB(RngColB.Count), _
       LookAt:=xlPart, SearchOrder:=xlByColumns)
End Sub

Sub ColorData()
   Do
       'In case of no data after First
       If Left(First.Offset(1), 4) = "Test" Then
           Set First = First.Offset(1)
           GoTo LoopAgain
       End If
       Call GetLast
       Select Case Right(First, 1)
           Case "1": ColorNum = 3
           Case "2": ColorNum = 5
           Case "3": ColorNum = 4
       End Select
       Set RngToColor = Range(First.Offset(1), Last)
       For Each i In RngToColor
           Range(i, Cells(i.Row, Columns.Count).End(xlToLeft)) _
               .Interior.ColorIndex = ColorNum
       Next i
       Set First = Last.Offset(1)
LoopAgain:
   Loop Until Last.Row >= LastRow
End Sub

Sub GetLast()
   For c = 2 To 1000
       If Left(First.Offset(c), 4) = "Test" Then
           Set Last = First.Offset(c - 1)
           Exit For
       Else
           If IsEmpty(First.Offset(c).Value) Then
               Set Last = First.Offset(c - 1)
               Exit For
           End If
       End If
   Next c
End Sub

> Yes it varies too. and for each Test X the color should be different.
>
[quoted text clipped - 77 lines]
>> >> >> > Thank you,
>> >> >> > Harsh

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.