Formatting an adjacent cell based on the content of another cell - excel

I believe this is probably an easy macro; it does not appear to be possible with conditional formatting and I have limited experience with macros.
I have many rows in an Excel workbook. What I'd like to is say:
If I place a < sign in the currently selected cell, then cell immediately to the right should format as bold, otherwise, it should not be emboldened.
I have fooled around with different "IF" statements, but I think I'm way off base.
How in the world do I do this?
EDIT
Here is perhaps a better way to show this, because for some reason I still cannot get it to work; I've included a screenshot.
(Actually, I realized that I asked the question backwards...it should be that if there if there is a cell with a "<" sign then the cell immediately to the right should not be bold).
Anyways, let's focus on row 12 for the sake of an example. It would be nice to have a formula to apply to the entire row that says "look at cell C12. If there is nothing in it, then D12 is emboldened. Look at cell E12, if there is nothing in it then F12 should be emboldened, etc. Look at cell Q12. If it has a "<" sign, then R12 should not be emboldened". The only exception to this would be cells that contain a "--"...those do not need to be emboldened.

With CF, select the range of cells to apply the formatting to - say Columns B:M (not A because that has no "immediately to the left") - HOME > Styles - Conditional Formatting, New Rule..., Use a formula to determine which cells to format and Format values where this formula is true::
=A1="<"
Format..., select Bold, OK, OK.
Edit By "those do not need to be emboldened" I take it you don't mind if they are. That might allow you to embolden the entire sheet (or rather ColumnsC:X) and apply a single conditional format. However that would result in the < themselves being emboldened and I take it you would prefer not. So if formatting is to be applied according to two separate conditions two separate rules are required. Select Columns D:X and for *bold**:
=C1=""
For Regular, with the same range selection:
=C1="<"

You're right, it is an easy macro. And #Scott Craner is correct as well; conditional formatting will work. However, there are times when you already have a lot of conditional formatting and adding another rule becomes a bit tricky, so here is the macro. Put it in the ThisWorkbook section.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Value = "<" Then
Sh.Cells(Target.Row, Target.Column + 1).Font.Bold = True
End If
End Sub
As you can see, you can make your own little formatting rules by checking the value of the cell that changed.
To learn more, search up "Learn Excel". Plenty of resources.

Related

Is changing the color of a cell based on nested conditionals possible?

I have been trying to change the color of certain cells on my WS that fall outside of an acceptable range. The statement I've been trying to use is,
IF(OR('cell'>1.3,'cell'<2.5),'turn red','do nothing')
In english, "If a cell is less than 1.3 or greater than 2.5 turn red, else do nothing." I cannot find a resource that guides me to how I can get the cells to change colors. I also have been unsuccessful in creating a statement in the Conditional Formatting tab that satisfies my goal. I feel this should be simple, but for some reason cannot figure it out. Does anyone know if this is possible, and if so how to do it?
EDIT: Also, I have different ranges based on another cell on the spreadsheet. I need this also taken into consideration
Select the columns that you want to format
Click Conditional format --> new rule
Select "Use a formula to determine which cells to format"
In the formula bar enter the formula =AND(A1>1.3,A1<2.5)
Choose the fill color as red and press OK
am confused with your formula and your english version. If its the other way, then use the formula =OR(A1<1.3,A1>2.5)

Drag formulas without "dragging"

I have few formulas in a row and I want to drag them down. Formulas are complex and I don't have them in R1C1 format and I don't want to convert them into R1C1, but I would like to do something like:
Range(A2:B10).Formula = Range.(A1:B1).Formula
I know this would put the same formula into the whole range, I need a "drag effect" - so it's changes relatively to each row but without copy-pasting.
P.S. I don't want to copy-paste, as it eats more CPU, I'd like to have something
Define the range that you want to fill with the formula and use Range.FillDown.
Range("A1:B10").FillDown
Note: The first Row(s) of the range must include the original formulas
A tiny trick !:
Sub KopyFormulas()
Range("A1:B1").Copy Range("A2:B10")
End Sub
will copy the formulas downward and adjust them as well !
For single cells, you don't need to explicitly convert formulas to R1C1 format to use FormulaR1C1.
Even if you are using the standard A1 style of referencing, you can still use
Range("B2:B10").FormulaR1C1 = Range("B1").FormulaR1C1
And this will have the effect of copying down the formula in B1. It doesn't change the default format -- they will still display in the A1 format.
There does seem to be an issue with doing this with a block of two or more cells. You could do it column by column (in a loop if desired):
Range("A2:A10").FormulaR1C1 = Range("A1").FormulaR1C1
Range("B2:B10").FormulaR1C1 = Range("B1").FormulaR1C1
Whether or not this is worth the hassle (compared to the simple .FillDown) is another question. You could time it.
By the way -- I wouldn't think that .FillDown is particularly inefficient. Did you do the usual optimizations of turning off of screen-updating, setting calculation to manual, etc.?

Excel Formula If Cell Contains String

I'm currently working in excel, and I'm trying to figure out a way to find if multiple cells contain the string value of another cell, and if it does highlight the cell where the row and column meet up. I created an example of what I want, only it will be on a much larger scale.
I've tried using: =ISNUMBER(SEARCH(substring,text)) but I'm not quite sure how to use it the way I want to.
Any help will be appreciated!
Your approach is correct, we can use the fact that conditional formatting is applied like dragging a formula, adapting relative references.
Create a conditional formatting formula rule:
=ISNUMBER(SEARCH(B$1,$A2))
Applied to B2:D7
Your formula will work nicely; what you'll want to do is put that formula into all the cells you want to highlight, so you get FALSE and TRUE in every cell.
You'll then use two Conditional Formatting rules. The first will look for Cell Value = TRUE, and will set cell background and font colour to yellow. The second will look for Cell Value = FALSE, and will set cell background to No Colour and Font to White.
This will reproduce the result you're looking for.
Edited to add:
It is possible to do this using just Conditional Formatting too, but it's a little more fiddly. If you want to try it, you can do this:
Highlight your range, and take note of which cell is Active - that's the cell within your highlighted range that is still white. It's also the one whose address is shown in the Name box in the upper left. For the sake of this answer, we'll assume that's B2
Create a new Conditional Formatting rule. Choose "Use a formula to determine which cells to format".
Use the formula =ISNUMBER(SEARCH(B$1,$A2). Set the format to colour just the cell background.
Note where the $ appears in the formula above - you want to leave the row number anchored in the first part, and the column letter anchored in the second part.
This takes advantage of the fact that Conditional Formatting is able to use absolute, relative, and mixed references to find which cells to format. It's also a tidier solution, but it can be harder to maintain if the sheet is ever repurposed or modified.

Writing a Conditional Format Formula in Excel

I am trying to write a rule in vba to trigger a Conditional Format, but I have my doubts if I am writing it correctly.
The condition for the format would be, if the cell value of a range is equal to the cell value of the same range on a different sheet:
=M7=Original!M7
This is to applied for all cells in a range (M7:BZ5000) [5000 is a large outer bound number to help conserve memory].
The issue that has me perplexed is how do I tell vba that M7=Original!M7, M8=Original!M8, Z143=Original!Z143, etc.
I feel this is a pretty basic concept but I searched these forums and could not find an answer that I think applies to my issue.
Also, Since I Would want this conditional format to trigger at the press of a button, how would I tell it to deactivate, WITHOUT removing the rest of my conditional formats (I have normal conditional formats applied to the range and want them to remain whenever the button isn't pressed.
Sheets("Main").Select
Range("M7:BZ5000").Select
For Each Cell In Selection
If Cell.Interior.Color = Excel.XlRgbColor.rgbBlack Then
Cell.Clear
End If
Next
You are using the conditional formatting choice of "Use formula to determine which cells to format"
The formula you will need is going to be like this (note the use of $ signs to control how the formula changes. The $ stops the value after it from changing)
=AND($M7=Original!$M7,$G$4="Y")
Note $G$4 is a cell you can use to turn on and off the cond. formatting. When it is "Y" the formatting will work, otehrwise any formatting you have applied will JUST be shown.
If you don't want you formatting to show when conditional formatting is on, then your conditional formatting MUST have formats the remove yours and sets them to the "default" settings.

Checking cell value dynamically

What I'm doing is a simple map on one tab of an office space that has all the cubicals laid out. On this map, I have conditional formatting checking another table of listen computers for the last time they were updated/maintained. It works as it stands, but we are moving into a new building and the map (which is already done) has WAY more cubes than last time. So I'm trying to figure out a more efficient way to do this task.
Here is the code:
=AND(VLOOKUP("CCA C1",LOCATION,3,FALSE)<>"",VLOOKUP("CCA C1",LOCATION,3,FALSE)<TODAY()-80)
So as it sits, it works fine. But what I'm trying to do is change the "CCA C1" to simply read the contents of the cell it's formatting so I can just format an entire range of cells rather than doing it cell by cell. The "CCA C1" is in the location of the second sheet (it's a named range). So this checks that entire range for "CCA C1" and checks if the date a couple columns over in that row is within 80 days, if it's not it highlights the cell in the map in red reminding us we need to check the computer. But what I would like to do is simply put "CCA C1" in the cell in the map (which is already is labeled), and have this check for the value of the cell it's formatting and look for that value the same way it's looking now. Just without me having to put "CCA C1" in the formula, I'd like to say something like this:
=AND(VLOOKUP(CURRENT CELL VALUE,LOCATION,3,FALSE)<>"",VLOOKUP(CURRENT CELL VALUE,LOCATION,3,FALSE)<TODAY()-80)
Make sense? Anyone know of a way to do this? Otherwise I have to conditionally format each individual cell with the value manually rather than just format all the cells with the same formatting and have the formula check the contents of the cell for what to look for in the location range of the other sheet.
And to clarify, I know that I can put in the actual cell, such as E3, but then I still have to manually change the formula for each cell which defeats the purpose. I want to just say current cell or something like that. I have 3 conditional formats for each cell, I have around 100 cells to be formatted, so rather than having 300 formats I have to put in, I'd love to just do 3. Not sure if this is possible, that's why I'm asking :)
Just replace "CCA C1" with the address of the first cell in the range of cells with the conditional formatting. Assuming your conditional formatting starts in B2:
=AND(VLOOKUP(B2,LOCATION,3,FALSE)<>"",VLOOKUP("CCA C1",LOCATION,3,FALSE)<TODAY()-80)
EDIT: As I commented, I'm not sure I understand the issue, but if I do, you need to enter the range of cells with CF in the applies to range of the CF dialog, rather than copying and using Paste Special:
Note that this works with the merged cells.
You will need to adjust the applies to range as you add more computers, etc., but the same formula will work.

Resources