+ Reply to Thread
Results 1 to 5 of 5

Count colourindex - VBA

Hybrid View

wrightyrx7 Count colourindex - VBA 02-27-2014, 07:27 AM
JOHN H. DAVIS Re: Count colourindex - VBA 02-27-2014, 07:46 AM
satabp Re: Count colourindex - VBA 02-27-2014, 07:48 AM
jmac1947 Re: Count colourindex - VBA 02-27-2014, 07:48 AM
wrightyrx7 Re: Count colourindex - VBA 02-27-2014, 07:58 AM
  1. #1
    Registered User
    Join Date
    03-31-2011
    Location
    Manchester
    MS-Off Ver
    Excel 2010
    Posts
    68

    Count colourindex - VBA

    Hi all,

    I have some code i tried to put together but it doesnt seem to be working.

    Can anyone see where im going wrong? Because when the colorindex IS equal to the values its not going to THEN

    Sub GenerateSick()
    Dim Sick, Sickness As Worksheet
    Dim g, r, a As Integer
    Dim lRow As Long
    Dim Rng, i As Range
    
    Set Sick = Sheets("Sick")
    Set Sickness = Sheets("Sickness (Confidential)")
    
        g = 0
        r = 0
        a = 0
    lRow = Sickness.Cells(Rows.Count, 13).End(xlUp).Row
    Set Rng = Sickness.Range("M7:M" & lRow)
        For Each i In Rng
            If i.Interior.ColorIndex = 4 Then g = g + 1
            If i.Interior.ColorIndex = 3 Then r = r + 1
            If i.Interior.ColorIndex = 45 Then a = a + 1
        Next i
    
    Sick.Range("B5").Value = g
    Sick.Range("B6").Value = a
    Sick.Range("B7").Value = r
    
    
    End Sub

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Count colourindex - VBA

    Maybe:

    Sub wrightyrx7()
    Dim Sick, Sickness As Worksheet
    Dim g, r, a As Long
    Dim i As Long
    
    Set Sick = Sheets("Sick")
    Set Sickness = Sheets("Sickness (Confidential)")
    
        g = 0
        r = 0
        a = 0
    Sickness.Activate
    For i = Range("M" & Rows.count).End(3)(1).Row To 7 Step -1
            If Range("M" & i).Interior.ColorIndex = 4 Then g = g + 1
            If Range("M" & i).Interior.ColorIndex = 3 Then r = r + 1
            If Range("M" & i).Interior.ColorIndex = 45 Then a = a + 1
    Next i
    
    Sick.Range("B5").Value = g
    Sick.Range("B6").Value = a
    Sick.Range("B7").Value = r
    End Sub
    Last edited by JOHN H. DAVIS; 02-27-2014 at 07:52 AM.

  3. #3
    Forum Contributor
    Join Date
    12-09-2013
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    171

    Re: Count colourindex - VBA

    Hi,

    The code is working fine..

    It is not going to "Then" because the condition was not satisfied... which means ColorIndex is not in 4, 3 and 45.
    Like my answer, then click * below

  4. #4
    Valued Forum Contributor
    Join Date
    01-19-2010
    Location
    Melbourne Australia
    MS-Off Ver
    latest is Excel 2016. have older versions
    Posts
    624

    Re: Count colourindex - VBA

    Hi wrightyrx7,
    is it not going to the "then" while stepping through with F8 or because the final assignments are not working??

    I a have seen experts on this forum differ on the results of your line
    Dim Sick, Sickness As Worksheet
    in terms of what "Sick" is defined as.

    I haven't tried to execute the code as I don't have a sample workbook and am happy for experts to suggest the right solution so I can learn some better practices as well

    Jmac1947

  5. #5
    Registered User
    Join Date
    03-31-2011
    Location
    Manchester
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: Count colourindex - VBA

    Hi guys,

    I have found its was a problem with the data that was being copied over.

    The colours were not in my colour palette.

    Added them and now it works fine.

    Thanks for looking at this for me.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 3
    Last Post: 11-18-2013, 02:06 AM
  2. [SOLVED] for every line I browse make a count and post this count in a cell (fiouuu)
    By historic777 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-30-2013, 06:41 AM
  3. Range.columns.count property returns wrong count after union operation
    By gopinan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-13-2008, 04:48 AM
  4. [SOLVED] Count Intervals of Filtered TEXT values in Column and Return Count across a Row
    By Sam via OfficeKB.com in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 PM
  5. [SOLVED] Count Intervals of Filtered TEXT values in Column and Return Count across a Row
    By Domenic in forum Excel Formulas & Functions
    Replies: 27
    Last Post: 09-06-2005, 04:05 AM

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