Solved

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

Posted on 2010-11-14
4
3,881 Views
Last Modified: 2013-12-21
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
0
Comment
Question by:bruingjt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 20

Expert Comment

by:ltlbearand3
ID: 34142474
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

0
 
LVL 1

Author Comment

by:bruingjt
ID: 34144771
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
0
 
LVL 20

Accepted Solution

by:
ltlbearand3 earned 500 total points
ID: 34152189
Joey,

I will need to know a few things to possibly help you more.  Are the rows and columns with data always the same (for example is the data always in A2:X200 or something like that)?  If the data is more dynamic we may have to change the macro some.  If it is static you can use these concepts:

The cell positions used in OOo Basic macros are based on cell A1 being position 0,0.  The first position is the column - making column D position 4.  The next position is the row.  Since this is a zero based array, you have to subtract one from the row number (therefore row two is listed as ,1).

In the Select Case Statment set a Case option for each possible value that Column D might posses - you mentioned 15 options.  Then set the appropriate background color using the RGB options - Red, Blue, Green.  Each value is on a scale of 0 to 255.  0,0,0 is black and 255, 255, 255 is white.  You could play around in OOo to find your colors (Tools >> Options >> OpenOffice.org >> Colors) or you could go to a web site like http://answers.oreilly.com/topic/453-color-names-and-rgb-values-with-wide-browser-support/ to get some ideas.

Below is some code that will loop through the first 11 rows of a sheet and change columns A through F based on a value in column D for that row.
Sub ConditionalFormat

	Dim oSheet as object
	Dim oCell as object
	Dim oCellD as object
	Dim intCol as integer
	Dim intRow as integer
	Dim lngColor as long
	
	' Get Access to active spreedsheet	
    oSheet = ThisComponent.CurrentController.ActiveSheet 
    
    'Loop through the row -- Set the end of the Loop to the Last Row of Data minus one
    For intRow = 0 to 10 	'This will go to Row 11
	    
	    ' Get the Value that is in Column D for this Row
	    oCellD = oSheet.getCellByPosition(3,intRow)
	    
	    ' Select the Proper color based on the value in Column D
	    Select Case oCellD.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(255, 255, 255)
	    		print lngColor
	    	Case "Value2"
	    		lngColor = RGB(200, 0, 100)
	    		print lngColor
	    	Case "Value3"
	    		lngColor = RGB(0, 0, 0)
	    		print lngColor
	    End Select
	
		' Loop Through Columns A through F
		For intCol = 0 to 6 
			oCell =  oSheet.getCellByPosition(intcol, intRow)	
			oCell.CellBackColor = lngColor
		Next
	Next

End Sub

Open in new window

0
 
LVL 1

Author Closing Comment

by:bruingjt
ID: 34152362
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
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question