• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4206
  • Last Modified:

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
0
bruingjt
Asked:
bruingjt
  • 2
  • 2
1 Solution
 
ltlbearand3Commented:
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
 
bruingjtAuthor Commented:
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
 
ltlbearand3Commented:
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
 
bruingjtAuthor Commented:
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now