Dynamically select the Conditional Formatting range based on a cell value - excel

looking for a conditional formatting trick to do this.
if value of Cell A1 = 1 then cell B1 should be Formatted. If the value of A1 = 2 then Cell B2.
conditional Formatting range should be changed dynamically based on value of cell A1.

This can be easily solved with Conditional Formatting, no need for VBA.
Mark your cells B1+B2, Choose Conditional Formatting (from Home-Tab), New Rule, Select "Use a formula...", enter =$A$1=ROW() as formula and set the Format to whatever you want.
UPDATE (after your comment what you want to format)
If the ranges you wan to format are always the same size and with the same distance, you can use a formula like
=AND(MOD(COLUMN()-1,4)>0,INT((COLUMN()-1)/4)+1=$A$1,ROW()>=5,ROW()<=10)
This checks if the row is between 5 and 10 and divide the column by 4 to check in which "block" you are. The Mod-part prevents the formatting of col A, E...
If the blocks you want to format are more complicated, you could solve this with an UDF:
Public Function calcFormattingVal(r As Range) As Integer
If Not Intersect(Range("B5:D10"), r) Is Nothing Then
calcFormattingVal = 1
ElseIf Not Intersect(Range("F5:H10"), r) Is Nothing Then
calcFormattingVal = 2
Else
calcFormattingVal = -999
End If
End Function
Now, put the following formula as conditional formatting. Be aware that you have to pass the address of upper left cell where your formatting starts as parameter to the function. So if you mark the range "B5:H12", use B5 as parameter.
=calcformattingval(B5)=$A$1

Related

Conditional formatting (Change Cell Color) if cell value <> ""

so to give some context, I have an application running where I have written code where basically, on a button press:-
A new sheet is created
The columns "A:L" in the new sheet are conditionally formatted
The condition is If any cell in column A has a value then that cell color is red. Similarly, a different color condition for every column
To achieve this, I wrote the following code snippet in my Sub:
Dim condA As FormatCondition
Set condA = Range("A5:A500").FormatConditions.Add(Type:=xlCellValue, Operator:=xlNotEqual, Formula1:="")
Now the problem is that the execution stops at the Set statement with the error:
Invalid procedure call or argument
What is the problem with the statements and how can you rewrite the code to change the interior color of any cell in the column if a value is entered or present in the cell?
Thanks!
If you wish to test for cells that are blank, use a formula of ="". To put that formula in as the Formula1 parameter, you would use Formula1:="="""""
Note: This won't strictly test for any value being entered by the user, because the user could enter a single ' into a cell, or they could insert a formula of ="" into a cell, and both those cells will not be coloured using your conditional format. That probably isn't likely to be an issue but, if it is, a Formula1 of "=AND(A5="""",A5=0)" (i.e. testing that the cell is both a blank string and a number zero - which should only be True if the cell is empty) might work. (Untested)

conditional formatting of a cell when a cell name is used on a formula inside that cell

I am trying to apply conditional formatting to a cell when it contains an equation with reference to a named cell.
Example: Cell I71 contains the following equation. =G71*Admin_Labor_Rate
Admin_Labor_Rate has a value of $50, and is the name of cell B152.
Now I want to find all cells that reference cell B152 "Admin_Labor_Rate" and apply conditional formatting (highlight). I have two other Labor Rates, so I want to use cell highlighting to distinctly identify if the labor is being performed by technicians, admin, or engineering.
I have done a lot of research and tried a lot of methods, but it seems as close as I can get is if the cell equates to a certain value, then I can format it.
Thanks in advance for any thoughts the community may have on this.
Use this formula with conditional formatting:
=ISNUMBER(SEARCH("Admin_Labor_Rate", FORMULATEXT(A1)))
Where A1 is the top-left cell of the range you want to conditionally format. For example, you select range F20:H30 and enter the formula with F20 instead of A1.
Perhaps a User Defined Function (i.e. UDF).
function formulaContains(rng as range, nr as string) as boolean
formulaContains= rng.hasformula and cbool(instr(1, rng.formula, nr, vbtextcompare))
end function

Excel: Custom a conditional format rule: if cells in colums C are not empty, then

I am trying to use a custom conditional format rule: if cells in column C are not empty, then color all other cells of the same line in green.
What should I enter in the cell of the Conditional Format Rule option? (http://hpics.li/9492511)
Should I use something like "not blank"?
Assuming headers are in row 1 and actual data starts in row 2, use this conditional format formula:
=AND($C2<>"",COLUMN(A2)<>3)
Apply it to the range that contains your data, in this example, I applied it to range $A$2:$E$21 and here is an image of what the result would look like:
Conditional formatting only works for the same cell, unfortunately.
You can use VBA to check the cell, and format other ones based on that formatting.
for(i = 0 to NUM_ROWS)
if cells(i,3).value = ""
for(j = 0 to NUM_COLS)
cells(i,j).Interior.Color = RGB(33, 173, 28)
next
end if
next
I haven't tested it, but it should be pretty close.
Actually, it looks like you could do this with conditional formatting.
for your formula you'd want
$C1 = ""
This should work for every value.

Excel formula that reads a color in one cell and puts a value in another based on that color

I am trying to set up an if statement formula that will read the color of one cell and place a value in another based on that color. I have tried writing several if statements to do this but cant find one to read the color. The sheet is set up to read a date cell. I have conditional formatting set up to color my weekends. I need a value "200" to show on days that are not on weekends and "0" to show on weekends.
If you were able to do the conditional formatting, you should be able to use a similar formula to input the value. I am guessing it reads the date cell, figures if it is the weekend or not and then inputs the value.
Assuming, for example, the non-weekends are Red (vbRed) then create a UDF (VBA, Add Module and paste the following:
Public Function checkColor(cell)
If cell.Interior.Color = vbRed Then
checkColor = 200
Else
checkColor = 0
End If
End Function
To use the function:
For example to check A1 and put 0 or 200 in B1, in B1 type
=checkColor(A1)

Dynamic Excel Subtraction

Say I've got cells A1-A10 populated with numbers. My initial formula in cell B1 is =A1-A6. However, I'd like to strike-out cell A3 (keeping the contents visible underneath the strike-out if possible), and I'd like the formula in B1 to recognise that change, and then automatically adjust itself to =A1-A7 (the idea being that I'd like A1 subtracted by the number in the cell 5 "non-struck out" cells below it). And then if I strike out cell A5 I'd like the formula to adjust itself to =A1-A8 and so on. Does anyone know how to do this?
(EDIT#1: misread the input, sorry)
A bit straightforward, but will do the job: type =A1-INDIRECT("A"&SMALL(IF(A:A<>"",ROW(A:A),""),6)) and press CTRL+SHIFT+ENTER instead of usual ENTER - this will define an ARRAY formula and will result in {} brackets around it (but do NOT type them manually!).
To speed up calculation you may replace A:A to any limited range.
Sample file (resulting formula is yellow-marked): https://www.dropbox.com/s/sy7zkg71xtfgib9/Subtract5th.xlsx
(EDIT#2: misread the "strike-out", sorry)
Font styles (as well as similar cell properties) may NOT be read by default Excel functions, that's why you need to add UDF called StrikeOut:
Press ALT-F11 - thiss will open VBA editor.
Insert new module: Insert > Module.
Paste the code to added module:
Function StrikeOut(R As Range) As Long
Dim c As Range
StrikeOut = 0
For Each c In R.Cells
If c.Font.Strikethrough = True Then StrikeOut = StrikeOut + 1
Next
End Function
Add the formula to B1: =A1-INDIRECT("A"&(6+StrikeOut(A2:A10)))
Set strikethrough font to any cells in A1:A10.
Unfortunately, cell format change does NOT trigger any change event, so you need either press F9 or change any cell value on the sheet to recalculate and therefore update result in B1.
Sample file is shared: https://www.dropbox.com/s/n9o7tn3ks3x8nza/StrikeOut.xlsm
P.S. at least for me that was extremely useful)))

Resources