Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculate the avg and color the cells when their values are above or below the avg
#1
Example:

Sub CalculateAverageAndColorCode()
    Dim dataRange As Range
    Dim avgValue As Double
    Dim cell As Range

    'Set the range where you want to calculate the average and apply color coding
    Set dataRange = Range("A1:A10")

    'Calculate the average value of the range
    avgValue = Application.WorksheetFunction.Average(dataRange)

    'Color code cells based on whether their value is above or below the average
    For Each cell In dataRange
        If cell.Value > avgValue Then
            cell.Interior.Color = RGB(0, 255, 0) 'Green color for above average
        ElseIf cell.Value < avgValue Then
            cell.Interior.Color = RGB(255, 0, 0) 'Red color for below average
        End If
    Next cell
End Sub

Explanation:

The code defines a subroutine named CalculateAverageAndColorCode.
It declares variables for the data range (dataRange), the average value (avgValue), and a loop variable (cell) to iterate through each cell in the range.
Set dataRange = Range("A1:A10") specifies the range A1:A10 where you want to calculate the average and apply color coding.
The Application.WorksheetFunction.Average function is used to calculate the average value of the dataRange.
The For Each loop is used to iterate through each cell in dataRange.
If a cell's value is above the average, it is colored green using cell.Interior.Color = RGB(0, 255, 0).
If a cell's value is below the average, it is colored red using cell.Interior.Color = RGB(255, 0, 0).
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Messages In This Thread
Calculate the avg and color the cells when their values are above or below the avg - by Qomplainerz - 07-27-2023, 10:41 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Format cells in a range with bold font and yellow background color Qomplainerz 0 772 07-27-2023, 11:26 AM
Last Post: Qomplainerz
  Split text in cells with VBA Qomplainerz 0 412 07-27-2023, 11:09 AM
Last Post: Qomplainerz
  Count colored cells with VBA Qomplainerz 0 354 07-27-2023, 11:07 AM
Last Post: Qomplainerz
  Calculate factorial of a given number in VBA Qomplainerz 0 386 07-27-2023, 11:04 AM
Last Post: Qomplainerz
  Protect specific cells with VBA Qomplainerz 0 377 07-27-2023, 10:39 AM
Last Post: Qomplainerz
  Calculate the sum of values in a column with VBA Qomplainerz 0 354 07-27-2023, 08:18 AM
Last Post: Qomplainerz
  Format cells with conditional formatting in VBA Qomplainerz 0 353 07-27-2023, 08:16 AM
Last Post: Qomplainerz
  Loop to fill numbers in cells with VBA Qomplainerz 0 375 07-27-2023, 08:16 AM
Last Post: Qomplainerz
  Calculate the factorial of a number with VBA Qomplainerz 0 354 07-27-2023, 07:56 AM
Last Post: Qomplainerz
  Calculate the average of three numbers in VBA Qomplainerz 0 321 07-27-2023, 07:40 AM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 6 Guest(s)