problems comparing cells in seperate columns

jeasterly

New Member
Joined
Oct 24, 2006
Messages
28
Hi all, I'm new to this but I can't find this specific question answered.

I have a spreadsheet that I am pulling into a workbook via macro that the data in Column B is the mfg cost : The data in Column C is the sales price.
I need the code to check if the value in C is 20% greater than the value in the corresponding B column. If it is NOT then shade the value in C yellow.

I can't use the conditional formatting since I am pulling the entire sheet in from an access query. Can anyone help. I am using Excel 2002.

Thanks so much!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Why can't you use conditional formatting?

=C2>B2*1.2
 
Upvote 0
Each time the macro is ran the sheet is pulled in by running a macro that runs another macro in access which runs the query that creates the sheet. I tried doing the conditional formatting but each time the macro runs the formatting is removed.
 
Upvote 0
Well just add code to do the conditional formatting to the macro.:)
 
Upvote 0
Sounds like a plan... Only I dont know how to write the code for the conditional formatting that I need.

thx
 
Upvote 0
Well it's probably worth recording a macro (Tools>Macro...Record New Macro...) when you do it manually.

That should generate code that you can integrate into the existing code.

Note it'll probably be altered, for example to take account of varying rows.

I'm just off now but give it a try and I'll check back later with some code.

By the way did you try the formula I posted?

If so did it work?
 
Upvote 0
Thanks Norie -
by doing the macro I was able to scarf up the code. I know there has to be a way doing this for each cell that is shorter than what I have here. Any tips?
Worksheets("CTO_Results").Range("N2").Select
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, _
Formula1:="=M2*1.2"
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
End With
Selection.FormatConditions(1).Interior.ColorIndex = 19
Worksheets("CTO_Results").Range("N3").Select
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, _
Formula1:="=M3*1.2"
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
End With
Selection.FormatConditions(1).Interior.ColorIndex = 19
Worksheets("CTO_Results").Range("N4").Select
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, _
Formula1:="=M4*1.2"
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
End With
Selection.FormatConditions(1).Interior.ColorIndex = 19
Worksheets("CTO_Results").Range("N5").Select
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, _
Formula1:="=M5*1.2"
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
End With
**************************etc....
Thanks for all your help.. I am new to this and just trying to fumble my way thru.
 
Upvote 0
jeasterly

Here's what I came up with based on your code.

I'm not 100% sure about the formula though.

Does it work for your data?
Code:
Dim ws As Worksheet
Dim LastRow As Long

Set ws = Worksheets("CTO_Results")

With ws
    LastRow = .Range("N" & Rows.Count).End(xlUp).Row
    With .Range("N2").Resize(LastRow - 1)
        With .FormatConditions
            .Delete
            .Add Type:=xlCellValue, Operator:=xlLessEqual, Formula1:="=RC[-1]*1.2"
            With .Item(1).Font
                .Bold = True
                .Italic = False
            End With
        End With
    End With
End With
 
Upvote 0
I get an error invalid outside procedure at the line
Set ws = Worksheets("CTO_Results")
and the SET is highlighted.
 
Upvote 0
Well that's probably because I ommitted the sub header.:oops:

I did that because I assumed you would be placing this in an existing sub.
 
Upvote 0

Forum statistics

Threads
1,214,559
Messages
6,120,194
Members
448,951
Latest member
jennlynn

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