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?