+ Reply to Thread
Results 1 to 7 of 7

Using VBA to add cell color backgrounds

Hybrid View

  1. #1
    Registered User
    Join Date
    09-24-2012
    Location
    Allentown, Pennsylvania
    MS-Off Ver
    Excel 2010
    Posts
    4

    Using VBA to add cell color backgrounds

    I have attached a spreadsheet that I created on Google Docs and exported to Excel. I used conditional formatting to color cells green or red, depending on whether or not it matched the answers in each cell of the top row. Green = correct, Red = wrong. My hope was to create a formula that would add all green cells in a row and tell me how many correct out of 19 questions a student in that row received. Ideally, I'd also like to create a formula in each column that told me how many students got that particular question wrong - but that's secondary to my need for a formula in each row.

    I have created a Macro to add cell colors, and created a formula to tally them. Unfortunately, every time I use the formula, it tabulates incorrectly. I have gone so far as clearing all conditional formatting rules and recreating them to make sure I did it correctly, and still I get weird results. For instance, there are 19 questions. The first student got 4 wrong, so the end result should be a 15. However, the student received a 17 according to the spreadsheet. What am I doing wrong!?!?

    *All names have been removed from the spreadsheet.*

    Using this formula instead of what I had works. I don't even need the Macro. Made more work than I needed to for this problem. Thanks for your help!

    =SUMPRODUCT(--(G2:Y2=$G$2:$Y$2)) - Values have been changed because my actual spreadsheet has cells that aren't hidden on this.
    Attached Files Attached Files
    Last edited by xman1x; 09-27-2012 at 12:38 PM. Reason: Resolved!

  2. #2
    Valued Forum Contributor
    Join Date
    03-17-2012
    Location
    Warsaw, Poland
    MS-Off Ver
    2007/2010
    Posts
    555

    Re: Using VBA to add cell color backgrounds

    it works just as you planned to. The conditional formating is still intact in the attached workbook. Get rid of it and the remaining cells which are red, wont be counted in in row B.

    You can't count the color of conditional formatted cells btw. At least not in the way you did it in your function (interior color).

    cheers

  3. #3
    Registered User
    Join Date
    09-24-2012
    Location
    Allentown, Pennsylvania
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Using VBA to add cell color backgrounds

    So by selecting the entire workbook and clearing conditional format is what you're saying? I tried that and it isn't working. There are no remaining red cells. Also, if I can't count with my function, how can I change it to count?

  4. #4
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Using VBA to add cell color backgrounds

    this is not the best option, but try
    Function getNumberOfCorrect(rng As range) As Long
    Dim r As range, count&
    For Each r In rng
        If InStr(r.Value, r.FormatConditions(2).Text) Then count = count + 1
    Next r: getNumberOfCorrect = count
    End Function

  5. #5
    Registered User
    Join Date
    09-24-2012
    Location
    Allentown, Pennsylvania
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Using VBA to add cell color backgrounds

    OK that did change my values, but instead of saying the students got a higher score then they deserved, now they are getting lower scores. You wanted me to just copy/paste the above as my new Macro, correct? Man this is confusing.

  6. #6
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Using VBA to add cell color backgrounds

    here is your file with "getNumberOfCorrect" UDF
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    09-24-2012
    Location
    Allentown, Pennsylvania
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Using VBA to add cell color backgrounds

    That still isn't working. The first row should have 15 correct. It is showing only 8 correct.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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