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,667 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
  • 2
  • 2
4 Comments
 
LVL 20

Expert Comment

by:ltlbearand3
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now