Link to home
Create AccountLog in
Avatar of bruingjt
bruingjt

asked on

How do I use Open Office Calc to change the background color of cell range A2:F2 based on the value of cell D2?

I want to change the background color of cells A2:F2 based on the value of cell D2.  The kicker is that cell D2 could be any of 15 values.  Is there a way to do this?

Thanks for your help in advance.....


Joey
Avatar of ltlbearand3
ltlbearand3
Flag of United States of America image

You will need to do this via a macro since conditional formatting in OOo can only handle three conditions.

Below is the start of a Macro that you can adapt to meet your needs.  You will need to add conditions to the Case statement for all your conditions and chosen background colors.  To insert the Macro into your document:

Press Alt=F11 for Macros (or from the menu Tools >> Macros >> Organize Macros >> OpenOffice.org Basic)
Click on your sheet name in the tree on the left.
Click the New Button
Enter a module name or leave it as Module1
Click OK
In the Editor, paste the code below over the default code
Save your document
Now modify the code to fit and save again

Let me know if you have questions
-Bear
Sub ConditionalFormat

	Dim oSheet as object
	Dim oCell as object
	Dim oCellD2 as object
	Dim intCol as integer
	Dim lngColor as long
	
	' Get Access to active spreedsheet	
    oSheet = ThisComponent.CurrentController.ActiveSheet 
    
    ' Build Case Statement to Set correct Color based on Cell
    oCellD2 = oSheet.getCellByPosition(3,1)
    
    ' Loop Through Cells A2 to F2
    Select Case oCellD2.String ' Use .String if the values are AlphaNumeric and .Value if Numeric Only
    	' Set lngColor equal to the RGB color tha you Want
    	Case "Value1"
    		lngColor = RGB(200, 200, 200)
    	Case "Value2"
    		lngColor = RGB(200, 0, 100)
    End Select

	' Loop Through Columns A through F
	For intCol = 0 to 6 
		oCell =  oSheet.getCellByPosition(intcol, 1)	
		oCell.CellBackColor = lngColor
	Next


End Sub

Open in new window

Avatar of bruingjt
bruingjt

ASKER

Okay, I think I'm able to follow what this is doing, and it works great for Row 2.  Forgive me though, but how can I make this loop through the entire sheet to set the color for each row based on the value in Column D?

Thanks,


Joey
ASKER CERTIFIED SOLUTION
Avatar of ltlbearand3
ltlbearand3
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Thank you so much.  That worked great!  Column D will always be the data check and the row will be colored based on the value of cell D in each row.  Thanks again!!!


Joey