Conditional formatting using formula with VBA?

Lil2606

Board Regular
Joined
Jan 29, 2019
Messages
79
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
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
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
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top