VBA for Excel 2007 tutorial-Below Average format


Below Average conditional format

To format cells contain values above the average value of the range of values, you need only add another line of VBA code to VBA code example of formatting values above the average (previous page) .For instance, you want to format the below average values in the range of cells from A1 to A10.


VBA for Excel: VBA example code to conditionally format below average cells

To run this VBA example code, you can copy and paste the VBA code below in to the block of Worksheet_Activate() procedure. To open this block, double-click the current worksheet, then in the objects dropdown list select Sheet1 and in events list select Activate.



VBA code for this example:



Option Explicit
Private Sub Worksheet_Activate()
    ConFormat
End Sub
 
Sub ConFormat()
 
    Dim cf As AboveAverage
    Dim i As Integer
    'Fill cells with random sample data from 1 to 100
    For i = 1 To 10
        Randomize
        Range("A" & i) = Int(100 * Rnd()) + 1
    Next
   
    'Select the range of cells to apply Above Average conditional format
    Range("A1:A10").Select
    'Create an AboveAverage object
    Set cf = Selection.FormatConditions.AddAboveAverage
    cf.AboveBelow = xlBelowAverage 'Specify below average formatting
    'Format the below average values with a mix of red and blue colors
    cf.Interior.Color = RGB(255, 0, 255)
      
End Sub

Comments





This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.