0

Referring to screen shot, I want to programmtically format a cell like so:

If cell B4 and cell F4 are both red text, then format cell J4 to be red text.

(and similarly for all other cells).

I see one can format a cell based on the value contained in another cell, but I'm not finding that you can base it on the format of the cell. Is this possible?

enter image description here


Add more info. As stated, I can see how to base a condition on a value of a cell. Here is an example:

Sub FormatUsingVBA()
Dim rng As Range
Dim lastRow As Long
lastRow = Cells(Rows.Count, 3).End(xlUp).Row
Set rng = Range("C2:C" & lastRow)For Each cell In rng
If cell.Value2 = "Adult" Then
Range(cell.Address).Offset(0, -2).Interior.ColorIndex = 3
ElseIf cell.Value2 = "KID" Then
Range(cell.Address).Offset(0, -2).Interior.ColorIndex = 4
ElseIf cell.Value2 = "Teenager" Then
Range(cell.Address).Offset(0, -2).Interior.ColorIndex = 6
Else
Range(cell.Address).Offset(0, -2).Interior.ColorIndex = 0
End If
Next cell
End Sub

So it has a condition based on the value in a cell (If cell.Value2 = "Adult"). I want base it on the format of a cell and I don't see how to do this.


Entering macro command in cell

Cell Formula

Macros

2
  • It looks like you are asking about Excel VBA. In that case, you should remove the tag for LO Calc. Or do you want to know how this would translate into LO Basic? It would need to be rewritten entirely.
    – Jim K
    Commented Mar 25, 2020 at 15:52
  • I can work with Excel or Calc. Commented Mar 25, 2020 at 16:04

1 Answer 1

1

From https://wiki.openoffice.org/wiki/Documentation/How_Tos/Calc:_STYLE_function:

There is no direct way to determine (in a formula) what formatting has been applied to a cell.

So it sounds like you need to write a macro, at least for Calc. No idea about MS Excel.

EDIT:

From my answer at https://ask.libreoffice.org/en/question/154239/get-color-of-text-in-cell/.

Function RGBprobe(x , y, optional z)
    Dim RGBarray(1 to 3)
    oDoc = ThisComponent
    oSheet = oDoc.Sheets(0)
    'Decreasing coordinate values by 1 because BASIC starts numbering with 0.
    If NOT IsMissing(z) Then oSheet = oDoc.Sheets(z-1)
    oCell = oSheet.getCellByPosition(x-1,y-1)
    CBkC = oCell.CharColor
    RGBarray(1) = Red(CBkC) : RGBarray(2) = Green(CBkC) : RGBarray(3) = Blue(CBkC)
    RGBprobe = RGBarray
End Function

For example, a formula that checks the color of cell A1 is =RGBPROBE(1;1). To see all three returned values, press Ctrl+Shift+Enter for the array formula result.

8
  • Yes, write a macro, that is what I meant by programmatically. Sorry if this was not clear. I've added more info to the question. Commented Mar 25, 2020 at 15:42
  • Thanks. So following the instructions in the link, I entered the call to the macro in the cell but it seems to be taking it as literal text, not a formula. What do I need to do? (see addendum to question) Commented Mar 25, 2020 at 16:27
  • It looks like you have a space in front of the formula, which apparently causes Calc to treat it as text.
    – Jim K
    Commented Mar 25, 2020 at 16:32
  • I just notice the red squiggle line under RGBProbe in the cell. It doesn't know what it is. Commented Mar 25, 2020 at 16:35
  • If the function wasn't defined, then it would say #NAME?. I think the problem is a space instead. You did add the function in My Macros, correct?
    – Jim K
    Commented Mar 25, 2020 at 16:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .