Need to select and highlight non-contiguous cells based on minimum value

ouadad

Board Regular
Joined
Jun 19, 2005
Messages
213
I've done some calculations where the output consists of 6 scenarios of 6000 rows of results in 12 separate worksheets. The output for each scenario looks like this, where columns B,C,D would contain values for RMSE, MAE and MASE for scenario 1 and columns G,H,I would contain the values for scenario 2, etc, for 6000 rows and 6 scenarios. What I would like to do is compare the values for RMSE in each row, find the minimum value and highlight the cell. Then do the same for MAE and MASE. The problem is the cells aren't contiguous so a Range over all the cells in the row won't give me what I want.

Code:
SES	RMSE	MAE	MASE		Mean	RMSE	MAE	MASE		Naive	RMSE	MAE	MASE
Test	8.331	8.331	0.458		Test	8.333	8.333	0.545		Test	0	0	0
Test	0.333	0.333	0.458		Test	0.333	0.333	0.6		Test	0	0	0
Test	2	2	0.512		Test	2	2	0.667		Test	2	2	0.512
Test	32.086	32.086	0.528		Test	32.083	32.083	0.75		Test	0	0	0
Test	0.417	0.417	0.458		Test	0.417	0.417	0.6		Test	0	0	0
Test	6.505	6.505	1.988		Test	3	3	0.667		Test	20	20	6.111
Test	0.167	0.167	0.458		Test	0.167	0.167	0.545		Test	0	0	0

I've written some code below just for the RMSE calculation just for the first three rows, but the rng search doesn't give me what I need.

Code:
Sub Highlight_Minimum_Metric()
Dim totalrows As Long, irow As Long, minRMSE As Double, rng As Range, cell As Range
totalrows = Sheets("h=1").UsedRange.Rows.Count
For irow = 2 To 4
    Set rng = Range(Cells(irow, 2), Cells(irow, 29))
     minRMSE = Application.WorksheetFunction.Min(Cells(irow, 2), Cells(irow, 7), Cells(irow, 22), Cells(irow, 27))
    For Each cell In rng
        If cell.Value = minRMSE Then
            cell.Font.Color = vbRed
         End If
        Next cell
Next irow
End Sub
 
Last edited:

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
This is done with Conditional Formatting. Does it do what you want?
- If so, it could be applied with vba if required.
- If not, what should be highlighted & why?

I selected B2:N8 and then set the Conditional Formatting formula rules.
This uses the MINIFS function which is only available in recent versions of Excel. If you don't have MINIFS but this is the result you want, post back for an alternative & state what Excel version you are using.

Excel Workbook
ABCDEFGHIJKLMN
1SESRMSEMAEMASEMeanRMSEMAEMASENaiveRMSEMAEMASE
2Test8.3318.3310.458Test8.3338.3330.545Test000
3Test0.3330.3330.458Test0.3330.3330.6Test000
4Test220.512Test220.667Test220.512
5Test32.08632.0860.528Test32.08332.0830.75Test000
6Test0.4170.4170.458Test0.4170.4170.6Test000
7Test6.5056.5051.988Test330.667Test20206.111
8Test0.1670.1670.458Test0.1670.1670.545Test000
Min
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B21. / Formula is =AND(B$1="MASE",B2=MINIFS($B2:$N2,$B$1:$N$1,"MASE"))Abc
B22. / Formula is =AND(B$1="MAE",B2=MINIFS($B2:$N2,$B$1:$N$1,"MAE"))Abc
B23. / Formula is =AND(B$1="RMSE",B2=MINIFS($B2:$N2,$B$1:$N$1,"RMSE"))Abc
 
Upvote 0
Peter, I'd really like to do it with VBA. I have 12 worksheets and each has six scenarios with 6000 rows. I know I can do it with conditional formatting, but I would rather do it through a macro.
 
Upvote 0
Peter, I'd really like to do it with VBA. I have 12 worksheets
I'm not sure if they are the only worksheets in your workbook? The code below is written to act on all sheets in the workbook, so if you have others, we need some detail to determine which ones to do and which ones to leave alone.

Also, I have not tested on any large data, so I would suggest trying with smallish sets of data to start with - & not any workbook that does not have a backup.

Code:
Sub Highlight_Minimums()
  Dim a As Variant, aRMSE As Variant, aMAE As Variant, aMASE As Variant, c As Variant, aCols As Variant, aColors As Variant
  Dim ws As Worksheet
  Dim h As Long, i As Long
  Dim dMin As Double
  
  aColors = Array(vbGreen, vbCyan, vbYellow)
  For Each ws In Worksheets
    With ws.UsedRange
      a = .Value
      ReDim aRMSE(0): ReDim aMAE(0): ReDim aMASE(0)
      For i = 1 To UBound(a, 2)
        Select Case a(1, i)
          Case "RMSE": ReDim Preserve aRMSE(1 To UBound(aRMSE) + 1): aRMSE(UBound(aRMSE)) = i
          Case "MAE": ReDim Preserve aMAE(1 To UBound(aMAE) + 1): aMAE(UBound(aMAE)) = i
          Case "MASE": ReDim Preserve aMASE(1 To UBound(aMASE) + 1): aMASE(UBound(aMASE)) = i
        End Select
      Next i
      aCols = Array(aRMSE, aMAE, aMASE)
      For i = 2 To UBound(a)
        For h = LBound(aCols) To UBound(aCols)
          dMin = Application.Min(Application.Index(a, i, aCols(h)))
          For Each c In aCols(h)
            If a(i, c) = dMin Then .Cells(i, c).Interior.Color = aColors(h)
          Next c
        Next h
      Next i
    End With
  Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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