is there a way I can store and later restore conditional formatting information? Upon selecting a cell, I want to
1/ store the condformatting of the corresponding column/row in some arrays
2/ delete the condformatting of the corresponding column/row
3/ change InteriorColor of column/row to something nice (so that I see a crosshair)
4/ upon selecting another cell, I want to restore the condformatting of the original column/row and repeat the process from 1/ for the currently selected cell.
I tried something like
Public condFmt1 As FormatCondition
Set condFmt1 = Range("A1").FormatConditions.Item(1)
...
Set Range("A2").FormatConditions.Item(1) = condFmt1
however the last line gives me a runTime error 438 - Object does support this property or method. Im probably assigning wrongly.
After playing around with it for an hour, I've found a way around using FormatCondition values in VBA.
This code copies my "A1" range's conditional formatting, which is set to change the font to bold if the value is TRUE
Sub CopyFormatCondition()
Dim fC As FormatCondition
Set fC = Range("A1").FormatConditions.Item(1)
Range("A2").FormatConditions.Add Type:=xlCellValue, Operator:=fC.Operator, Formula1:=fC.Formula1
Range("A2").FormatConditions.Item(1).Font.Bold = fC.Font.Bold
End Sub
Some properties such as Type, Operator and Formula1 have to be set when the FormatCondition is being initialised, hence why I had to add a FormatCondition to the range's FormatConditions collection.
Other properties (e.g. Font and Interior) can be set after the FormatCondition's Initialization.
Assuming you're using Excel 2007 or higher, you can achieve your goal also with a Non-VBA solution:
Use an empty cell in your worksheet/workbook as a trigger cell and set it to TRUE. For convenience, name this cell DisableConditionalFormatting or similar.
Select the active area of the worksheet (Ctrl-A) and insert a new rule -> use formula to determine which cells to format. As a formula set =DisableConditionalFormatting. No need to alter the format here.
After setting the new rule, make sure to select the checkbox Stop if true in the conditional formatting rule manager
No you can toggle the conditional formatting for the worksheet on and off by changing the cell DisableConditionalFormatting.
Related
I am having an issue converting my specific formula parameters into VBA code. Ive tried many ways but there always seems to have an issue.
When opening conditional formatting box, I have:
Formula= J5>0. Format: Black font Red fill. Applies to: =$K$5:$K$509.
Ive achived the final result, all I need is to somehow write this into VBA
Here are some hints for your VBA coding:
To get an object representing a range use the Range function: Set myRange = Range("K5:K509") and to avoid ambiguity you can use the Range method defined on the Worksheet class: Set myRange = myWorksheet.Range("K5:K509)"
To access conditional formatting use Range.FormatConditions, e.g. to add a new conditional formatting: Set myFC = myRange.FormatConditions.Add(xlExpression, Formula1:="=...")
To change the style of the cell if the condition matches use the resulting object: myFC.Interior.Color = vbRed
Can you use Excel to store a condition requiring the an object to be inserted in a different field. Example if - cell B1 text = "yellow" then place "Caution Clip Art" in cell B30 and make the background yellow.
I'm thinking of JavaScript where you can just use an "if statement". But I've never run across needing this until now. Open to alternatives for a fairly large spreadsheet wanting to use a graphic or clip art.
Graphic Sat Level
You could just use Conditional Formatting for this:
=IF(B1="yellow",1,if(B1="green",2,0))
Then, highlight the column. On the Home Tab > Conditional Formatting > Icon Sets > and choose one that fits your needs.
If you don't want to see the 0,1,2 values, set them to whatever background color you want. And if you want to set the background to yellow, you can use another Conditional format: Conditional Formatting > Highlight Cell Rules and set it up to color how you want.
I believe you need a user-defined function for this. Luckily, it's a simple one. Make sure it's within a Module.
Function ShowPic(PicFile As String) As Boolean
Dim AC As Range
On Error GoTo Done
Set AC = Application.Caller
ActiveSheet.Shapes.AddPicture PicFile, True, True, AC.Left, AC.Top, 200, 200
ShowPic = True
Exit Function
Done:
ShowPic = False
End Function
You can then call this function with =ShowPic("pathtofile.jpg"). You could also use it within an IF statement. So =IF(A2="yellow",ShowPic("caution.jpg"),IF(A2="green",ShowPic("greenlight.jpg")...
The background color can be changed with conditional formatting.
I am building a residential stacking plan in which each cell = unit type with specific color given conditional formatting.
2 tables follow below for each unit reflect a sqm size and a $ value.
I need to reflect only the cells' color into the following tables.
I need a dynamic solution and would prefer avoiding vba (since I'm not proficient), but will use if necessary. Thanks in advance!
Find Image HERE
Set up your Conditional formatting as normal on the first table like this:
Note my table starts at cell C4 but yours is in a different place and should be adjusted accordingly. make sure you DON'T have the $ symbol on the formula in the rule but you do have it on the 'Applies to' section
Now copy and paste this formatting onto the second table.
Finally edit the formulas in the conditional formatting so that they point to the starting cell of the FIRST table. It should look like this:
Note that the formatting 'Applies to' the second table but refers in the Formula to the values in the first table.
The result is this:
You can repeat this for other tables if you need to.
As you are working in Excel 2003(!), follow the following steps:
Select the cells in the second table.
In the menu, choose Format - Conditional Formatting.
In the Conditional Formatting box, choose Formula Is.
In the text box, enter the cell reference of the FIRST table (eg C4="4+"), do not enter any $ symbols.
Click the Format button and select the background fill to match the one in the first table.
Add the other conditions in the same way by clicking the Add>> button.
If you change the color code values (B21:B26) from 2 rooms to 2 (to match your second table), the following should do the trick. Basically, this code is not using conditional formating. Getting the color from conditional formating can be somewhat laborious and tricky (google "excel vba find color conditional formatting"). Instead, the present code reads the color in your Color Code cells, and apply it to the other two ranges.
Private Sub BckgndColor()
Dim ColorCodeRange As Range
Dim NoOfRooms As Range
Dim CellColorIndex As Integer
Dim c As Range
Dim d As Object
Set ColorCodeRange = Worksheets("Sheet1").Range("B21:B26")
Set d = CreateObject("scripting.dictionary")
'Add the pairs (value, color) to dictionary
For Each c In ColorCodeRange.Cells
d.Add c.Value, c.Interior.ColorIndex
Next
Set NoOfRooms = Worksheets("Sheet1").Range("M25:V36") 'Here the range of Table 2 (M25:V36 in your example)
'Scan range, and assign color
For Each c In NoOfRooms.Cells
If d.Exists(c.Value) Then
c.Interior.ColorIndex = d(c.Value)
c.Offset(16, 0).Interior.ColorIndex = d(c.Value) 'If Table 3 is always 16 rows down, this shoud work
End If
Next
Set d = Nothing
End Sub
I gave the option data validation list in sheet 2, while I selecting the option by list the cells will change . for that cells I want to get color also from source table in sheet1 to sheet2 .
What I would like to have is:
IF A1 in Sheet 2 is blue
Then A1 in Sheet 1 changes to blue
I know I can get the color of A1 in Sheet 2 by using:
=GET.CELL(63,Sheet2!A1)
(Excel: Can I create a Conditional Formula based on the Color of a Cell?)
But I can't figure out what I should do in the next step.
Update on 12.01.2015
At the beginning I thought a function would work, but as I considered my file, VBA may be needed.
It is about the output of a correlation analyse from SPSS, there are three columns: correlation coefficient, p-value and sample size.
I need to check the coefficient and p-value at the same time, and present the coefficient in a readable way. Say I run a correlation between 50 variables with 100 variables, I would not paste coefficient and p-value in one sheet, rather:
sheet one : coefficient
sheet two: p-value
What I would to have is:
If value of p-value is bigger than 0.05, then coefficient (cell) changes to blue/dark blue or black.
So that when I watch the first sheet, I know blue ones should be ignored because of non-significance.
What you need is a way to detect changes in cell format.
There appears to be no event that triggers upon change in format. See
How to detect changes in cell format?
I will describe a workaround, almost step-by-step. It is not keystroke-by-keystroke, so you may have to google a bit, depending on you background knowledge.
The description is not short, so please read it through.
You have to:
Detect change in Selection (there is an event for this).
Inquire about the color of your source cell.
Act if needed.
Go to the Visual Basic Editor (VBE) and add code in three modules:
A standard module (say, Module1). You have to first insert the module.
ThisWorkbook.
Sheet2.
In Module1:
Public prev_sel As Range
Public wssrc As Worksheet, wstrg As Worksheet
Public ssrc As String, strg As String
Public rngsrc As Range, rngtrg As Range
Sub copy_color(rngs As Range, rngt As Range)
Dim csrc As Long
csrc = rngs.Interior.Color
If (csrc = vbBlue) Then
rngt.Interior.Color = vbBlue
End If
End Sub
Sub copy_color2(rngs As Range, rngt As Range)
If (TypeName(prev_sel) = "Range") Then
Dim pss As String
pss = prev_sel.Parent.Name
If (pss = ssrc) Then
Dim ints As Range
Set ints = Application.Intersect(rngs, prev_sel)
If (Not (ints Is Nothing)) Then
Call copy_color(rngs, rngt)
End If
End If
End If
End Sub
In ThisWorkbook:
Private Sub Workbook_Open()
ssrc = "Sheet2"
strg = "Sheet1"
Set wssrc = Worksheets(ssrc)
Set wstrg = Worksheets(strg)
Set rngsrc = wssrc.Range("A1")
Set rngtrg = wstrg.Range("A1")
Call copy_color(rngsrc, rngtrg)
If (TypeName(Selection) = "Range") Then
Set prev_sel = Selection
Else
Set prev_sel = Nothing
End If
End Sub
In Sheet2:
Private Sub Worksheet_Deactivate()
Call copy_color(rngsrc, rngtrg)
If (TypeName(Selection) = "Range") Then
Set prev_sel = Selection
Else
Set prev_sel = Nothing
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Call copy_color2(rngsrc, rngtrg)
If (TypeName(Target) = "Range") Then
Set prev_sel = Target
End If
End Sub
I will soon edit with explanations. Reading carefully, it can be readily understood, though.
Notes:
This code does not act if the source cell color changes from vbBlue to something else. You did not specify anything for this action. Actually, your specification was not detailed enough to cover all possible cases.
There might be cases (extremely unlikely, I guess) where this code fails. For instance, if color is changed via other VBA code, without selecting/deselecting cells.
The idea is to check for the need of acting after as many relevant events as possible. Here I am detecting Workbook_Open, Worksheet_Deactivate, Worksheet_SelectionChange. You may add other events with suitable Subs, e.g., Workbook_BeforeClose, Workbook_BeforeSave. All this is a way of substituting for the non-existing event of changing cell format.
I like the answer by pnuts (although I did not have time to test it). But the present one gives a flexibility that is not available with the other. There might be some cases (depending on what you need to do) that would not be covered by it.
There are other possible combinations of places to locate variables declaration and other code, essentially performing the same actions.
Not recommended because of reliance on the XLM (not XML) Macro function GET.CELL. This from a technology introduced 30 years ago that was effectively superseded eight years later. With almost all its elements now defunct, the few that remain can be expected to have a low life expectancy. Microsoft encourages migration to VBA.
Nevertheless, you have asked ‘how’ rather than ‘why not’, so I suggest you proceed from where you have reached and select Sheet1 A1 and HOME > Styles - Conditional Formatting - New Rule..., Use a formula to determine which cells to format, Format values where this formula is true:
=CellColor=23
and select blue formatting of your choice, OK, OK, Apply.
23 is a fairly standard number for Blue (not Light, not Dark) but your configuration may expect a different number.
Note that another disadvantage is that, unlike CF in general, the response is not automatic – you may need to enter something in Sheet1 A1, or Shift+F9 to force an update.
If your data is spread across two sheets (Sheet1 and Sheet2, both ColumnA) and there is a 1:1 relationship (the p-value in A1 of Sheet2 is that for the correlation coefficient in A1 of Sheet1) then a simple Conditional Formatting rule may suffice:
Select Sheet1 ColumnA and HOME > Styles - Conditional Formatting, New Rule...
Use a formula to determine which cells to format
Format values where this formula is true:
=Sheet2!A1>0.05
Format..., select dark blue or to suit, OK, OK.
The same rule might be applied in Sheet2 (ColumnA) in the same way so the cells (by row) conditionally formatted in one sheet are those conditionally formatted in the other.
The GET.CELL function, whilst useful, comes from the old XLM macro language which was used before VBA. You may encounter restrictions using this e.g. at that time Excel used a limited number of colours (I have read somewhere around 60?).
Alternatively, with a bit of VBA, you could experiment with the Interior Object and also the Font Object :
Sheets("Sheet1").Range("A1").Interior.Color = vbBlue
Sheets("Sheet1").Range("A1").Font.Color = vbYellow
If Sheets("Sheet1").Range("A1").Interior.Color = vbBlue Then _
Sheets("Sheet2").Range("A1").Interior.Color = vbBlue
If Sheets("Sheet1").Range("A1").Font.Color = vbYellow Then _
Sheets("Sheet2").Range("A1").Font.Color = vbYellow
You will likely need to explore the various ways of specifying the colors to use to give you maximum control/flexibility.
Just to be clear and to keep the functionality you deliver simple, you could use conditional formatting and choose to set the format with a colour. This is incredibly easy once you know how. The main trick is what formula to enter and specifically which cell you need a conditional formats formula to reference when the conditional format applies to a multi cell range.
As an example. If your conditional formatting rule is created such that it applies to the range $C$5:$C$10 the formula you use will often need to be entered as =(A5="A"). Note this is a relative addressing formula ie no dollar signs. this has the effect of the cell c6 inspecting the value of a6 etc.
Your only complication now is to inspect the formatting of the cell rather than the value it stores. In 2013, you can still use =GET.CELL(63,A5) to do this, however this can't be entered in the formula of the CF rule ... Other posts discuss the whys and wherefores of using this. See this link which described how to get cell info.
So you'll end up with a formula in a cell next to the cell that has the colouring. The formula will use a named range that returns true or false depending on whether the colour of the cell matches the colour you specify in the named range. You conditional formatting on another sheet will reference this formula cell and set the colour of the new cell.
You would use the following formula in the named range called "Get .
=GET.CELL(65,OFFSET(INDIRECT("RC",FALSE),0,1))
I've got this to work, and the key information can be found one the referenced web site page.
Ok?
I have an add-in xla file that I use to store my regularly used VBA code. This function is stored in the add-in modules.
Public Function IsFormula(cell_ref As Range)
IsFormula = cell_ref.HasFormula
End Function
This correctly returns True or False if I type it into cell: =IsFormula(A1)
However, when I try to create a new formatting rule using the formula option, I get this error 'You cannot use references to other worksheets or workbooks for Conditional Formating criteria.' The error is not because of quotation marks.
There isn't exactly a clear question to be answered here, but if you want to format say all of the cells in Sheet1 that contain formulae then, in Sheet1 A1:
1) define a name (say ‘Formulaic’, with ‘Sheet1’ for “Scope” and =GET.CELL(48,A1) for “Refers to”.
2) Select Sheet1
3) Set required conditional format with “Use a formula to determine which cells to format” and =Formulaic in “Format values where this formula is true:”
The ‘type_num’ (eg 48 above) is described at http://www.mrexcel.com/forum/excel-questions/20611-info-only-get-cell-arguments.html