+ Reply to Thread
Results 1 to 5 of 5

Thread: can't figure how to count # of cells minus red cells VBA

  1. #1
    Registered User
    Join Date
    08-11-2009
    Location
    Mesa, Arizona, USA
    MS-Off Ver
    Excel 2003
    Posts
    75

    can't figure how to count # of cells minus red cells VBA

    Hi I have tried to figure out how to count all cells in a column and subtract any cells that contain characters with a red font. I'll attach an example. I need code that will work in VBA so that in the case that the column does not contain any cells with red numbers it won't come up with a debug error.

    In excel I normally do this manually with this formula: =count("") I normally highlight all cells except the red ones.
    See attachment for example.

    Also, the # of rows is never the same. Some files will be 100 rows, some will be 1000, depending on the store's file we get. so the code would probably have to contain something like .lastactiverow or something. (sorry I'm new to VBA and don't know much)

    Thanks in advance!
    Curbster
    Attached Files Attached Files
    Last edited by curbster; 08-18-2009 at 05:21 PM.

  2. #2
    Forum Moderator Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    5,953

    Re: can't figure how to count # of cells minus red cells VBA

    Hi Curbster,

    The following code will count all cells in column C that don't have a red font color and then display the non-red count in a message box once finished. You could, of course, have the count entered into a cell on the worksheet instead.
    Sub countNonRed()
    Dim i As Long, cellCount As Long
    For i = 1 To Range("C65536").End(xlUp).Row
        If Range("C" & i).Font.ColorIndex <> 3 Then
            cellCount = cellCount + 1
        End If
    Next i
    MsgBox cellCount
    End Sub

  3. #3
    Registered User
    Join Date
    08-11-2009
    Location
    Mesa, Arizona, USA
    MS-Off Ver
    Excel 2003
    Posts
    75

    Re: can't figure how to count # of cells minus red cells VBA

    Thanks, that gives me the message box as you stated which is great. I guess I don't understand how to get that number to the cell I need...

    the number needs to be in column C, the second cell below the last active row. any ideas?

    thanks!

  4. #4
    Forum Moderator Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    5,953

    Re: can't figure how to count # of cells minus red cells VBA

    Sure, change:
    MsgBox cellCount
    to
    Range("C" & Range("C65536").End(xlUp).Row + 2).Value = cellCount

  5. #5
    Registered User
    Join Date
    08-11-2009
    Location
    Mesa, Arizona, USA
    MS-Off Ver
    Excel 2003
    Posts
    75

    Re: can't figure how to count # of cells minus red cells VBA

    That works! thanks!

+ 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.2.0