Type Mismatch Error (Error 13) in a SELECT/CASE Routine (Nested For Each Loop) for Conditional Formatting

messadua

New Member
Joined
Mar 5, 2022
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
Hello,

I am trying to define a VBA MAcro for Excel 2019. I have a reasonably big dataset in which every column (variable) has numbers with different scales and meanings. Would like to automate conditional formatting
so to loop through each column in the range of interest, identify empty cells as well as values > 95ht percentile and < 5th percentile and highlight all of them.

This is the code I have come up with

Sub PreProcessBeta()

Dim sRow As Long ' Starting Row
Dim sCol As Long ' Starting Column
Dim lRow As Integer ' Last Row of the Analysis
Dim lCol As Integer ' Last Column of the Dataset
Dim rCol As Range ' Current Column under Analysis
Dim rng As Range ' Final Dynamic Range
Dim c As Range
Dim rCol_95 As Variant ' 95th Percentile of rCol
Dim rCol_5 As Variant ' 5th Percentile of rCol


' Hard Code First Row and First column of interest
sRow = 2
sCol = 5

' Define Last Row, Dynamically
lRow = Range("A1048576").End(xlUp).Row
'use the lRow to help find the last column in the range
lCol = Range("XFD" & lRow).End(xlToLeft).Column
Set rng = Range(Cells(sRow, sCol), Cells(lRow, lCol))

'First delete the formatting conditions for the range to start fresh.
rng.FormatConditions.Delete

'Start the First Loop: Select entire active Column and compute Percentiles of Interest, for each active column
For Each rCol In rng.Columns

rCol_95 = Application.WorksheetFunction.Percentile_Inc(rCol, 0.95)
rCol_5 = Application.WorksheetFunction.Percentile_Inc(rCol, 0.05)

For Each c In rCol
' For Each cell in the active Column (rCol is the entire Column under analysis (a Range))

Select Case c.Value

Case Is = "."
c.Interior.ColorIndex = 35 ' light green for missing values
c.Font.Bold = False

Case Is >= rCol_95
c.Interior.ColorIndex = 38 ' light pink for values over the 95th percentile
c.Font.Bold = True

Case Is <= rCol_5
c.Interior.ColorIndex = 37 ' light blue for values over the 25th percentile
c.Font.Bold = True

Case Else
c.Interior.ColorIndex = 2
c.Font.Bold = False

End Select

Next c

Next rCol


End Sub

The dynamic range selection part opf the macro seems working fine but when I run the entire code I get a "Type Mismatch Error (Error 13), apparently related to the content of the c.Value
In fact, any occurrence of the Case Is command produce the same error (either ".", >= rCol_95 or even <= rCol_5).

Any help or advice would be really really appreciated,

Thank you in advance,

Diego


Following you find a minimal version of the dataset for testing purposes

Test.xlsm
ABCDEFGHIJKLMN
1nrecpidtimewaveclinicagebsaecho_avelecho_ealatecho_eameanecho_earatecho_easeptecho_edectecho_eearatecho_evel
2111VIT000001582252.001.830.200.130.093.700.04173.008.710.74
3212VIT000001582252.001.850.390.100.081.720.06184.008.400.67
4313VIT000001582252.001.830.470.100.091.230.07242.008.400.58
5414VIT000001582252.001.830.510.120.091.120.06200.008.400.57
6515VIT000001582252.001.830.470.100.091.300.07249.007.180.61
7621VIT000001772976.002.010.690.060.061.100.06247.0012.670.76
8722VIT000001772976.002.000.780.060.060.960.06242.0012.500.75
9823VIT000001772976.002.000.760.030.060.750.09278.009.500.57
10924VIT000001772976.001.980.750.040.040.850.04220.0016.000.64
111025VIT000001772976.002.001.000.030.040.560.05282.0014.000.56
121126VIT000001772976.001.930.790.040.040.680.04238.0013.500.54
131231VIT000003695579.001.530.200.100.073.450.04224.009.860.69
141332VIT000003695579.001.480.340.040.041.590.03253.0015.430.54
151433VIT000003695579.001.47.0.040.03.0.02217.0022.000.66
161534VIT000003695579.001.51.0.040.03.0.02238.0022.330.67
171641VIT000002801461.001.760.900.070.070.780.07198.0010.000.70
181742VIT000002801461.001.760.580.070.070.860.07262.007.690.50
191843VIT000002801461.001.760.800.060.060.840.05154.0012.180.67
201944VIT000002801461.001.790.960.070.060.590.05176.009.500.57
212045VIT000002801461.001.79........
222146VIT000002801461.001.790.580.070.060.930.05167.009.000.54
232247VIT000002801461.001.790.790.090.070.800.05226.009.000.63
242351VIT000003267163.001.640.700.100.100.860.10215.006.000.60
252452VIT000003267163.001.64........
262553VIT000003267163.001.660.640.090.070.730.05226.006.710.47
272654VIT000003267163.001.670.450.110.081.890.04135.0011.330.85
282755VIT000003267163.001.680.550.060.060.780.05250.007.820.43
292856VIT000003267163.001.680.670.100.070.660.04121.006.290.44
302957VIT000003267163.001.660.720.100.070.710.04234.007.290.51
313061VIT000003885273.001.530.630.070.070.790.07227.007.140.50
323162VIT000003885273.001.630.360.070.051.720.05263.0012.400.62
333263VIT000003885273.001.620.480.080.080.880.07274.005.600.42
343364VIT000003885273.001.590.360.070.071.000.06122.005.540.36
353465VIT000003885273.001.630.850.100.090.540.07235.005.410.46
Sheet1
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hi and welcome to MrExcel.

What you have in 'c' in this line: For Each c In rCol is range of the entire column of 'rCol'.
To read a cell you need to iterate through all the cells in the column:

Change this
VBA Code:
For Each c In rCol

For this:
VBA Code:
For Each c In rCol.Cells
 
Last edited:
Upvote 0
Hi and welcome to MrExcel.

What you have in 'c' in this line: For Each c In rCol is range of the entire column of 'rCol'.
To read a cell you need to iterate through all the cells in the column:

Change this
VBA Code:
For Each c In rCol

For this:
VBA Code:
For Each c In rCol.Cells
Easy, but Effective and Wonderful fix, it is now working like a champ !

Thank you so very much, you made my day and saved me a lot of work :)
 
Upvote 0
Im glad to help you, thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,960
Messages
6,122,479
Members
449,088
Latest member
Melvetica

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