Results 1 to 5 of 5

Excel - VBA Comparing cell values to each column's average

Threaded View

  1. #1
    Registered User
    Join Date
    10-05-2014
    Location
    United States
    MS-Off Ver
    2013
    Posts
    3

    Excel - VBA Comparing cell values to each column's average

    I'm trying to prepare a code in vba for excel. For each month there is a list of values, each month is listed in a separate column, multiple values are listed below each month.

    What I wanted to do is change the color of each cell which value is 4 times smaller than the month's average. So far my code looks like this, each average is calculated like I wanted it, but i can't seem to figure out how to get it to compare the cells only to one column's average, not to all of them consecutively - like it does now.
    Sub MonthAverage()
        Dim Data As Variant
        Set Data = Selection
        Dim rows As Integer
        Dim columns As Integer
    
        rows = Data.rows.Count
        columns = Data.columns.Count
    
        Dim average As Double
    
        For j = 1 To columns
            average = 0#
    
            For i = 1 To rows
                average = average + Data.Cells(i, j)
            Next
    
            average = average / rows
    
            For Each cell In Data.Cells
                If cell.Value <= average / 4 Then cell.Interior.Color = RGB(0, 0, 255)
            Next
    
            MsgBox average 'just to make sure I got the averages correctly
        Next
    End Sub
    Last edited by alansidman; 10-05-2014 at 12:31 PM. Reason: code tags added

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 19
    Last Post: 03-15-2014, 04:50 PM
  2. Excel 2010 Comparing column values
    By Bill001 in forum Excel General
    Replies: 7
    Last Post: 08-14-2013, 01:32 AM
  3. [SOLVED] Conditional Formatting: Comparing cell with a column of values and applying 3 rules
    By ab231 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-02-2013, 10:30 AM
  4. Replies: 3
    Last Post: 10-15-2012, 10:42 AM
  5. Grouping rows based on column values and comparing row values
    By bernborough in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-27-2010, 10:18 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1