Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
KAH KAH is offline
external usenet poster
 
Posts: 6
Default function to colour cells

I want to write a function to colour a given cell based on the value of a
variables. Below if what I have so far. But I am not sure how to define the
function or how to use cells in functions. Any assistance is appreciated.

If wsResults.Cells(x, y).Value < (z) Then
Cells(x,y).Select
With Selection.Interior
.ColorIndex = 37
.Pattern = xlSolid
End With
ElseIf wsResults.Cells(x,y).Value (z) Then
Cells(x,y).Select
With Selection.Interior
.ColorIndex = 36
.Pattern = xlSolid
End With
End If

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default function to colour cells


hi,


Try these examples for colouring cells base on selected/defined
ranges...



Code:
--------------------
Option Explicit
Sub Test()
Dim wsResults As Worksheet
Set wsResults = ActiveSheet
'example one
x = 1
y = 3
Call ColourCell(wsResults.Cells(x, y), 5)
'example two
Call ColourCell(wsResults.Range("a4: b10"), 5)
Set wsResults = Nothing
End Sub

Sub ColourCell(rng As Range, z As Long)
Dim Cll As Range
Dim CllColour As Long
Application.ScreenUpdating = False
For Each Cll In rng
With Cll
Select Case .Value
Case .Value < z
CllColour = 36
Case .Value z
CllColour = 37
End Select
With .Interior
.ColorIndex = CllColour
.Pattern = xlSolid
End With
End With
Next Cll
Application.ScreenUpdating = True
End Sub

--------------------


hth
Rob


--
broro183

Rob Brockett. Always learning & the best way to learn is to
experience...
------------------------------------------------------------------------
broro183's Profile: http://www.thecodecage.com/forumz/member.php?userid=333
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=105261

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default function to colour cells


hi,

When reviewing my last post I noticed a potential for error if the
value is equal to z. To overcome this the Select case statement could be
modified to...


Code:
--------------------

Select Case .Value
Case .Value < z
CllColour = 36
Case .Value z
CllColour = 37
Case .Value = z
CllColour = 40
Case Else 'eg if there is non-numeric/text string in a cell
CllColour = 50
End Select

''alternatively you could merge the "=z" line with one of the others ie
' Case .Value <= z
' Case .Value = z

--------------------


hth
Rob


--
broro183

Rob Brockett. Always learning & the best way to learn is to
experience...
------------------------------------------------------------------------
broro183's Profile: http://www.thecodecage.com/forumz/member.php?userid=333
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=105261

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
Changing all cells in one colour to a different colour Bob Excel Discussion (Misc queries) 3 June 25th 08 02:12 PM
use "countif" function to count cells by colour ken Excel Worksheet Functions 1 December 12th 07 08:22 AM
i want to use the =if function to apply a colour to other cells pookie1970 Excel Worksheet Functions 1 June 20th 05 05:53 PM
Adding colour to a range of cells based on one of the cells v... McKenna Excel Discussion (Misc queries) 4 March 11th 05 02:25 PM
How can I change the colour of cells using an IF function? gctexcel Excel Worksheet Functions 1 January 10th 05 12:38 PM


All times are GMT +1. The time now is 09:41 AM.

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"