Excel user defined function if cell is highlighted then - excel
I have created a user defined function to determine whether cells are highlighted a particular color, and it works in some situations, but not the one I need; Basically it works when that cell is permanently highlighted that color but I am basing it on conditional formatting and the function doesn't work if the cell is that color due to conditional formatting.
Public Function Active(Rng As Range) As Boolean
If Rng.Interior.Color = RGB(217, 151, 149) _
Then Active = True
End Function
Any help as to why would be much appreciated.
For simple Conditional Formatting (NONE-Color scaling type), you can refer to #brettdj and #Chuff 's external reference
http://www.xldynamic.com/source/xld.CFConditions.html#specific
How do I find the fill colour value of a conditionally formatted cell in Excel 2007 using vba?
For color scaling Conditional formatting, I have tried pasteSpecial but it's not working.
The only workable solution for me is to copy the range into MS Word, and then copy the range back into EXCEL. This will remove the conditional formatting. Then you can get the background color as
Cells(X,Y).interior.color
Or else you need to implement the color scaling type of conditional formatting yourself.
Can you perhaps use the same criteria in your udf as used by the conditional formatting? Instead of checking for color, check the factors the conditional format checks for.
Related
VBA Conditional Formatting - xlExpression - Formula
I am new to VBA, and I have searched online but have yet not succeeded and would like to ask kindly for your support. I would like to create an Excel Macro which turns cell entries red, when they do not meet a specific set of conditions (VAT Format). My research indicates that the following should VBA code could work: Set condition1 = rng.FormatConditions.Add(xlExpression, Formula1: "XXX") Defining and setting the format to be applied for each condition With condition1 .Font.Color = vbBlue .Font.Bold = True End With I have written the conditional formatting code here: =OR(AND(LEFT(B10,2)="CZ",OR(ISNUMBER(VALUE(RIGHT(B10,8))),ISNUMBER(VALUE(RIGHT(B10,10))),ISNUMBER(VALUE(RIGHT(B10,9)))),OR(LEN(B10)=10,LEN(B10)=11,LEN(B10)=12)),AND(ISNUMBER(VALUE(RIGHT(3))),MID(B10,12,1)=".",ISNUMBER(VALUE(MID(B10,9,3))),MID(B10,8,1)=".",ISNUMBER(VALUE(MID(B10,5,3))),MID(B10,4,1)="-",LEFT(B10,3)="CHE",LEN(B10)=15),AND(LEFT(B10,2)="BG",OR(ISNUMBER(VALUE(RIGHT(B10,9))),ISNUMBER(VALUE(RIGHT(B10,10)))),OR(LEN(B10)=11,LEN(B10)=12)),AND(LEFT(B10,2)="BE",ISNUMBER(VALUE(RIGHT(B10,10))),LEN(B10)=12),AND(LEFT(B10,3)="ATU",ISNUMBER(VALUE(RIGHT(B10,8))),LEN(B10)=11),AND(LEFT(B10,2)="DE",ISNUMBER(VALUE(RIGHT(B10,9))),LEN(B10)=11),OR(AND(LEN(B10)=9,ISNUMBER(VALUE(RIGHT(B10,8))),LEFT(B10,1)="X"),AND(LEN(B10)=9,ISNUMBER(VALUE(LEFT(B10,8))),RIGHT(B10,1)="X"),AND(LEN(B10)=9,LEFT(B10,1)="X",RIGHT(B10,1)="X",ISNUMBER(VALUE(MID(B10,2,7))))),AND(LEFT(B10,2)="EL",ISNUMBER(VALUE(RIGHT(B10,9))),LEN(B10)=11),AND(LEFT(B10,2)="EE",ISNUMBER(VALUE(RIGHT(B10,9))),LEN(B10)=11),AND(LEFT(B10,2)="DK",ISNUMBER(VALUE(RIGHT(B10,8))),LEN(B10)=10),AND(LEFT(B10,2)="HR",ISNUMBER(VALUE(RIGHT(B10,11))),LEN(B10)=13),AND(LEFT(B10,2)="GB",ISNUMBER(VALUE(RIGHT(B10,9))),LEN(B10)=11),OR(AND(ISNUMBER(VALUE(RIGHT(B10,9))),NOT(ISNUMBER(VALUE(MID(B10,3,1)))),LEFT(B10,2)="FR",LEN(B10)=13),AND(ISNUMBER(VALUE(RIGHT(B10,9))),NOT(ISNUMBER(VALUE(LEFT(B10,4)))),LEFT(B10,2)="FR",LEN(B10)=13),AND(ISNUMBER(VALUE(RIGHT(B10,10))),NOT(ISNUMBER(VALUE(LEFT(B10,3)))),LEFT(B10,2)="FR",LEN(B10)=13),AND(ISNUMBER(VALUE(RIGHT(11))),LEFT(B10,2)="FR",LEN(B10)=13)),AND(LEFT(B10,2)="FI",ISNUMBER(VALUE(RIGHT(B10,8))),LEN(B10)=10),AND(LEN(B10)=14,LEFT(B10,2)="NO",ISNUMBER(VALUE(MID(B10,3,9))),RIGHT(B10,3)="MVA"),AND(LEN(B10)=14,LEFT(B10,2)="NL",ISNUMBER(VALUE(MID(B10,3,9))),MID(B10,12,1)="B",ISNUMBER(VALUE(RIGHT(B10,2)))),AND(LEFT(B10,2)="LU",ISNUMBER(VALUE(RIGHT(B10,8))),LEN(B10)=10),AND(LEFT(B10,2)="IT",ISNUMBER(VALUE(RIGHT(B10,11))),LEN(B10)=13),AND(LEFT(B10,2)="IL",ISNUMBER(VALUE(RIGHT(B10,8))),ISNUMBER(VALUE(RIGHT(B10,8)))),OR(AND(NOT(ISNUMBER(VALUE(RIGHT(B10,2)))),ISNUMBER(VALUE(LEFT(B10,7))),LEN(B10)=9),AND(ISNUMBER(VALUE(MID(B10,3,4))),ISNUMBER(VALUE(LEFT(B10,1))),NOT(ISNUMBER(RIGHT(B10,1))),NOT(ISNUMBER(VALUE(MID(B10,2,0)))),LEN(B10)=8),AND(NOT(ISNUMBER(VALUE(RIGHT(B10,1)))),ISNUMBER(VALUE(LEFT(B10,7))),LEN(B10)=8)),AND(LEFT(B10,2)="HU",ISNUMBER(VALUE(RIGHT(B10,8))),LEN(B10)=10),AND(ISNUMBER(VALUE(RIGHT(B10,8))),LEFT(B10,2)="MT",LEN(B10)=10),OR(AND(ISNUMBER(VALUE(RIGHT(12))),LEFT(B10,2)="LT",LEN(B10)=14),AND(ISNUMBER(VALUE(RIGHT(B10,9))),LEFT(B10,2)="LT",LEN(B10)=11)),AND(ISNUMBER(VALUE(RIGHT(B10,11))),LEFT(B10,2)="LV",LEN(B10)=13),AND(NOT(ISNUMBER(RIGHT(B10,1))),ISNUMBER(VALUE(MID(B10,3,8))),LEFT(B10,2)="CY",LEN(B10)=11),AND(ISNUMBER(VALUE(RIGHT(B10,10))),LEFT(B10,2)="SK",LEN(B10)=12),AND(ISNUMBER(VALUE(RIGHT(B10,7))),LEFT(B10,2)="SI",LEN(B10)=10),AND(ISNUMBER(VALUE(RIGHT(B10,10))),LEFT(B10,2)="SE",LEN(B10)=14),OR(AND(ISNUMBER(VALUE(RIGHT(B10,2))),LEFT(B10,2)="RO",LEN(B10)=4),AND(ISNUMBER(VALUE(RIGHT(B10,3))),LEFT(B10,2)="RO",LEN(B10)=5),AND(ISNUMBER(VALUE(RIGHT(B10,4))),LEFT(B10,2)="RO",LEN(B10)=6),AND(ISNUMBER(VALUE(RIGHT(B10,5))),LEFT(B10,2)="RO",LEN(B10)=7),AND(ISNUMBER(VALUE(RIGHT(B10,6))),LEFT(B10,2)="RO",LEN(B10)=8),AND(ISNUMBER(VALUE(RIGHT(B10,7))),LEFT(B10,2)="RO",LEN(B10)=9),AND(LEN(B10)=10,LEFT(B10,2)="RO",ISNUMBER(VALUE(RIGHT(B10,8)))),AND(ISNUMBER(VALUE(RIGHT(B10,9))),LEFT(B10,2)="RO",LEN(B10)=11),AND(ISNUMBER(VALUE(RIGHT(B10,10))),LEFT(B10,2)="RO",LEN(B10)=12)),AND(ISNUMBER(VALUE(MID(B10,3,9))),LEFT(B10,2)="PT",LEN(B10)=11),AND(ISNUMBER(VALUE(MID(B10,3,10))),LEFT(B10,2)="PL",LEN(B10)=12)) How can I incorporate my conditions into the VBA code with conditional formatting turning cell entries red if they do not meet the above conditions? Your support would be highly appreciated. Many thanks.
There are two ways to modify the format of a cell: conditional formatting and VBA. Using VBA, you write a macro like this: IF InStr("CZ", Range("B10").Value) ... THEN Range("..."). ' change the background colour Using conditional formatting, you might apply a formula (or another rule) on a cell. What you are doing, is writing a VBA macro where you, instead of colouring the cell, you want to apply conditional formatting, based on a complicated formula, which should then take care of the colouring. Why are you making that combination?
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.
Formatting an adjacent cell based on the content of another cell
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.
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.
Special Cells with Specific Format
In a range of 10 cells i have 2 cells of red colors. Like I can identify the blank cells using below Dim rng As Range Set rng = Range("A1:A10").SpecialCells(xlCellTypeBlanks) rng.Select Is there a way to identify cells of specific format (in this case cells with Red color) as specialcells does not show any relevant option. I want to identify these red cells in a range preferably without any loop. Thanks for looking into it.
altering a color in a cell is non-volatile - that means that cells depending on the color will not be updated if their reference cell changes. Having said that, and also having recommended against using the color to make a decision (when you should try to use the value) you could try the below: see Using old XLM GET.CELL (function 63) function to reference color use that function to create an #NA value in the cell (so =IF(...,NA(),"") maybe? Use SpecialCells to return the cells with #NA (see here for some tips) I hope that gets you started, at least with some reading that might point you in the right direction. But again, you should try to make your decision based on a cells value, not formatting