Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1
Default Help with filling cells with colour

I've been trying to get this to work for hours, can anybody see where I'm
going wrong (by the way its my first dabble in trying to use colours in
VBA).

I want to put numeric values of say X and Y into A1 and A5 and then run a
function (fill_cell_sub) when I load the worksheet such that if X Y, the
cells A6 to A11 are filled in with one colour (colour 1) but if X<Y they are
filled in with another colour that I define in the call.

Below is my current version.

Function fill_cell_sub(value1, value2, cell_index, colour_cell)

Dim i As Integer
Dim Range_new As Range

' Define the initial cell in the block and make it active
Range(cell_index).Offset(0, 0).Select
'
If (value1 value2) Then
For i = 1 To 5
Set Range_new = Range(cell_index).Offset(-1 + i, 0)
Range_new.Select
Range_new.Interior.ColorIndex = 1
Selection.Interior.Pattern = xlSolid
Next i
ElseIf (value1 <= value2) Then
For i = 1 To 5
Set Range_new = Range(cell_index).Offset(-1 + i, 0)
Range_new.Select
Range_new.Interior.ColorIndex = colour_cell
Selection.Interior.Pattern = xlSolid
Next i
End If
'
End Function

Any help on where I'm going wrong would be much appreciated.

Steve D



  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 10,593
Default Help with filling cells with colour

Why not use conditional formatting? See
http://www.contextures.com/xlCondFormat01.html

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"Steve D" wrote in message
...
I've been trying to get this to work for hours, can anybody see where I'm
going wrong (by the way its my first dabble in trying to use colours in
VBA).

I want to put numeric values of say X and Y into A1 and A5 and then run a
function (fill_cell_sub) when I load the worksheet such that if X Y, the
cells A6 to A11 are filled in with one colour (colour 1) but if X<Y they
are filled in with another colour that I define in the call.

Below is my current version.

Function fill_cell_sub(value1, value2, cell_index, colour_cell)

Dim i As Integer
Dim Range_new As Range

' Define the initial cell in the block and make it active
Range(cell_index).Offset(0, 0).Select
'
If (value1 value2) Then
For i = 1 To 5
Set Range_new = Range(cell_index).Offset(-1 + i, 0)
Range_new.Select
Range_new.Interior.ColorIndex = 1
Selection.Interior.Pattern = xlSolid
Next i
ElseIf (value1 <= value2) Then
For i = 1 To 5
Set Range_new = Range(cell_index).Offset(-1 + i, 0)
Range_new.Select
Range_new.Interior.ColorIndex = colour_cell
Selection.Interior.Pattern = xlSolid
Next i
End If
'
End Function

Any help on where I'm going wrong would be much appreciated.

Steve D





  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,058
Default Help with filling cells with colour

You basically have the right idea. However a function can only the the value
of the cell into which it is entered. It cannot change formats. If you want
to modify formats you can:

1. use conditional formatting
2. use a sub type macro
--
Gary''s Student - gsnu2007


"Steve D" wrote:

I've been trying to get this to work for hours, can anybody see where I'm
going wrong (by the way its my first dabble in trying to use colours in
VBA).

I want to put numeric values of say X and Y into A1 and A5 and then run a
function (fill_cell_sub) when I load the worksheet such that if X Y, the
cells A6 to A11 are filled in with one colour (colour 1) but if X<Y they are
filled in with another colour that I define in the call.

Below is my current version.

Function fill_cell_sub(value1, value2, cell_index, colour_cell)

Dim i As Integer
Dim Range_new As Range

' Define the initial cell in the block and make it active
Range(cell_index).Offset(0, 0).Select
'
If (value1 value2) Then
For i = 1 To 5
Set Range_new = Range(cell_index).Offset(-1 + i, 0)
Range_new.Select
Range_new.Interior.ColorIndex = 1
Selection.Interior.Pattern = xlSolid
Next i
ElseIf (value1 <= value2) Then
For i = 1 To 5
Set Range_new = Range(cell_index).Offset(-1 + i, 0)
Range_new.Select
Range_new.Interior.ColorIndex = colour_cell
Selection.Interior.Pattern = xlSolid
Next i
End If
'
End Function

Any help on where I'm going wrong would be much appreciated.

Steve D




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
cell auto filling with colour Ruggy Excel Discussion (Misc queries) 4 January 24th 07 02:46 PM
filling cells Art Excel Discussion (Misc queries) 0 November 30th 06 03:36 PM
filling cells widman Excel Discussion (Misc queries) 0 November 29th 06 07:29 PM
filling cells Art Excel Discussion (Misc queries) 0 November 29th 06 06:35 PM
Filling empty cells with a value Ian Richardson ACITP Excel Discussion (Misc queries) 4 May 17th 06 03:25 PM


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