Conditional formatting using vba

Sayan2795

New Member
Joined
Sep 8, 2018
Messages
13
Hi ,
So I created an automated summary sheet which extracts data from multiple sheets and puts it in one workbook. I need to do conditional formatting on the data in summary sheet based on certain parameters
BaseUpperLowerPROD APROD BPROD CPROD DPROD E
2456.16.37.17.47.5
The above table shows the basic structure. As it is an automated machine each time the rows and columns will change depending on the number of files from where we take data.

What I need to do :-
Formulae1 = Base + Upper
Formula 2 = Base - Lower

I need to check in a row if the value lies between the two formula ie if Base+ upper > PROD A > Base - lower
If it does it needs to be marked with red.
We have to check for all the products and there are multiple rows with different Base , upper and lower value.

Please help. I need to write a VBA code for this.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
See if this does what you want. Test in a copy of your workbook.

Code:
Sub UpperLower()
  Dim a As Variant
  Dim i As Long, j As Long, uba2 As Long
  Dim uppr As Double, lowr As Double
  
  With Range("A1").CurrentRegion
    a = .Value
    uba2 = UBound(a, 2)
    For i = 2 To UBound(a)
      uppr = a(i, 1) + a(i, 2)
      lowr = a(i, 1) - a(i, 3)
      For j = 4 To uba2
        If a(i, j) >= lowr Then
          If a(i, j) <= uppr Then
            .Cells(i, j).Interior.Color = vbRed
          End If
        End If
      Next j
    Next i
  End With
End Sub

This is my sample data and results. If it is not what you expect, please give more details.


Book1
ABCDEFGH
1BaseUpperLowerPROD APROD BPROD CPROD DPROD E
22456.16.37.17.47.5
3103115122912
424.256.190-2.3-4
Sheet1
 
Upvote 0
Or using the in-built conditional formatting.
Note I've strictly used the same comparisons as in the question. So does NOT colour entries equal to the limit.
This doesn't have a loop, which should be fast to apply - though might slow the worksheet if you have a huge number of rows.
Maybe not relevant, but using formulas it will be dynamic - if values in columns A:C change.

Code:
Sub maybe()

    With Range("A1").CurrentRegion.Offset(, 3).Resize(, 5)
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(RC1+RC2>RC,RC>RC1-RC3)"
        .FormatConditions(1).Interior.ColorIndex = 3
    End With

End Sub
 
Upvote 0
I am getting a type mismatch error in line uppr = a(I,1) +a(I,3)
When there is more than 1 respondent in the thread, it is a good idea to make it clear who you are addressing. :)
That actually isn't even a line in my code but never-the-less the error you are reporting could indicate that at least some of the "numbers" in columns A:C are not straight-forward numbers. There may be error values or text characters that perhaps you cannot see.
Could something like that be possible?

Does your table actually start in column A?

If you start a brand new workbook and manually type in the sample data the same as mine and put the code in that workbook and run it what happens?
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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