Conditional formatting using formula with VBA?

Lil2606

Board Regular
Joined
Jan 29, 2019
Messages
76
Hi all,

I have a sheet generated by a software every month, so the layout is the same but more or less rows. For this I am writing a code to format it for us as we need it, to make reports from it, so we don't have to format it manually, and I am trying to add some conditional formatting to it but its just not exactly right.. can I get some help please?

So I have a column with "Something"s in it and I have a column with "Country"-ies in it, and it should never be possible to have "Something1" or "Something2" written beside anything else, only "Country1" or "Country2", but sometimes there are typos and they write the wrong "Something" to some countries and I'm trying set up a conditional formatting rule to highlight it for us if somewhere in the sheet there is "Something1" or "Something2" written beside a country that is not "Country1" or "Country2". I hope this makes sense.

So here is my code to add this conditional formating:
Code:
[/COLOR][COLOR=#333333]Sub ConditionalF()[/COLOR]
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">
Dim x As Long
Dim myrownr As Long
Dim mycolnr As Long
Dim myotrcolnr As Long

myrownr = Sheet1.ListObjects("Table1").ListRows(1).Range.Row

mycolnr = Sheet1.ListObjects("Table1").ListColumns("Something").Range.Column

myotrcolnr = Sheet1.ListObjects("Table1").ListColumns("Country").Range.Column

Sheet1.ListObjects("Table1").Range(myrownr, mycolnr).Select
    
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=AND(OR(" & Sheet1.ListObjects("Table1").Range(myrownr, mycolnr) & "=""Something1""," & Sheet1.ListObjects("Table1").Range(myrownr, mycolnr) & "=""Something2""),AND(" & Sheet1.ListObjects("Table1").Range(myrownr, myotrcolnr) & "<>""Country1""," & Sheet1.ListObjects("Table1").Range(myrownr, myotrcolnr) & "<>""Country2""))"

x = Sheet1.ListObjects("Table1").DataBodyRange.FormatConditions.Count

Selection.FormatConditions(x).Interior.Color = RGB(255, 255, 0)
 </code>[COLOR=#333333]End Sub[/COLOR][COLOR=#333333]

Now my idea was to write it only to the first row of my table, and then copy the formatting which does the job perfectly when I do it manually, but the problem is, that this VBA code retunrs the actual string written into the cell ...Range(myronr, mycolnr), ...Range(myronr, myotrcolnr) so when I run this and go to manage rules under conditional formatting, I get this formula in the formula bar: =AND(OR(Something45="Something1",Something45="Something2"),AND(Country3<>"Country1",Country3<>"Country2"))


and thats not right.. I would need to get the cells address - C2 instead of Something45 and - D2 instead of Country3 but I don't know how to do that...

Is there a way to do this? Could someone help me please?

Thank you!

This question was also posted on ExcelForum, with an example workbook as well. Link:
https://www.excelforum.com/excel-pr...th-vba-with-example-and-code.html#post5091398
 

Some videos you may like

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Lil2606

Board Regular
Joined
Jan 29, 2019
Messages
76
Hi ya! I have the solution. Here it is:

Code:
Sub ConditionalF()

Dim x As Long
Dim myrownr As Long
Dim mycolnr As Long
Dim myotrcolnr As Long


myrownr = Sheet1.ListObjects("Table1").ListRows(1).Range.Row


mycolnr = Sheet1.ListObjects("Table1").ListColumns("Something").Range.Column


myotrcolnr = Sheet1.ListObjects("Table1").ListColumns("Country").Range.Column




Sheet1.ListObjects("Table1").Range(myrownr, mycolnr).Select
    
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=AND(OR(" & Cells(myrownr, mycolnr).Address(RowAbsolute:=False, ColumnAbsolute:=False) & "=""Something1""," & Cells(myrownr, mycolnr).Address(RowAbsolute:=False, ColumnAbsolute:=False) & "=""Something2""),AND(" & Cells(myrownr, myotrcolnr).Address(RowAbsolute:=False, ColumnAbsolute:=False) & "<>""Country1""," & Cells(myrownr, myotrcolnr).Address(RowAbsolute:=False, ColumnAbsolute:=False) & "<>""Country2""))"


x = Sheet1.ListObjects("Table1").DataBodyRange.FormatConditions.Count


Selection.FormatConditions(x).Interior.Color = RGB(255, 255, 0)


Sheet1.ListObjects("Table1").Range(myrownr, mycolnr).Copy
Sheet1.ListObjects("Table1").ListColumns(mycolnr).DataBodyRange.PasteSpecial xlPasteFormats




End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,109,487
Messages
5,529,162
Members
409,851
Latest member
Ingar
Top