VBA Conditional Formatting - xlExpression - Formula - excel
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?
Related
Why does my conditional formatting not working?
The conditional formatting is and the worksheet looks like this Formula for the AL / MC: =IF(ISBLANK(F7),F8,IF(AND(NOT(ISBLANK(F8)),EXACT(F7,F8)),F7,CONCATENATE(F7,CHAR(10),F8))) For the above image, I have done the conditional formatting on the worksheet so that different category is highlighted but for the one with AL/MC together is not following the conditional formatting, any idea why is it so? I tried to redo the conditional formatting and search for solutions online but just cannot get the AL/MC to change accordingly.
Conditional formatting on cells to check if they are equal
I am trying to apply a formula to change color of both cells if they are not equal and if they are equal. I tried conditional formatting in excel but it did not give me the required solution. I want something like below: please click to see the image Thanks in advance.
I think you need to apply dollars in the conditional formatting formula. The dollars need to be in front so the formatting is applied properly. Select the range you want to apply conditional formatting > Conditional formatting > New Rule > Use a formula to determine which cells to format: And do it again but then with <> in between and make it red. There you go.
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.
Excel user defined function if cell is highlighted then
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.