+ Reply to Thread
Results 1 to 4 of 4

Thread: Count and Highlight text cells

  1. #1
    Registered User
    Join Date
    07-22-2011
    Location
    Eindhoven, Hollan
    MS-Off Ver
    Excel 2003
    Posts
    2

    Unhappy Count and Highlight text cells

    Hello,

    I'm new to the forum but the search option didn't work so I started a new thread.

    The worksheet I'm working on contains A:M columns with data. Every row contains information about one component. I want to no how many components I have of different types. So how many components are of CASE A or CASE B, etc. This description can be stated in one of the 22 columns, but there also the possibility that a description is stated twice in the same row. The description can be surrounded by other text.

    The main idea I had was to count the cells containg this data and highlight the rows with this data. And if a discription is stated twice in a row then the color changes again. But I can't get it to work and there must be an easier way around it. So can some help me?

    This is what I have so far (I warn you I'm new to vba so it might suck ):
    Private Sub Worksheet_Activate()
    
        Dim component As Range
        Dim total_comp As Integer
      
        Set component = ActiveSheet.Range("A4:M65536")
        For Each cell In component
            If cell = "*CASE A*" Then
                total_comp = total_comp + 1
                    If cell(component).Interior.ColorIndex = 3 Then
                    cell(component).Interior.ColorIndex = 4
                    Else: cell(component).Interior.ColorIndex = 3
                    End If
            End If
        Next cell
        cell("C2") = total_comp
    End Sub
    thanks a lot!

  2. #2
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,276

    Re: Count and Highlight text cells

    Hi mtx_22-3, I've reworked your code a little, the search is by no means fast because of the search criteria but it works.
    Private Sub Worksheet_Activate()
        Dim component As Range, cell As Range, Found As Range
        Dim total_comp As Integer
        Set component = ActiveSheet.Range("A4:M65536")
        For Each cell In component
            Set Found = cell.Cells.Find("*CASE A*")
            If Not Found Is Nothing Then
                Debug.Print cell.Address
                total_comp = total_comp + 1
                cell.Interior.ColorIndex = 4
            Else: cell.Interior.ColorIndex = 3
            End If
        Next cell
        Cells(2, 3).Value = total_comp
    End Sub
    Please leave a message after the beep!

  3. #3
    Forum Guru Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,023

    Re: Count and Highlight text cells

    Just a few tweaks, untested but try this
    code supplied by Simon Lloyd
    '22/07/2011
    'Microsoft Office Help
    Private Sub Worksheet_Activate()
    
        Dim component As Range
        Dim total_comp As Long, cnt As Long
        cnt = 0
        Set component = ActiveSheet.Range("A4:M" & ActiveSheet.Range("M" & Rows.Count).End(xlUp).Row)
        For Each cell In component
            If cell = "*CASE A*" And cnt = 0 Then
                total_comp = total_comp + 1
                    If cell(component).Interior.ColorIndex = 3 Then
                    cell(component).Interior.ColorIndex = 4
                    Else: cell(component).Interior.ColorIndex = 3
                    End If
                    cnt = cnt + 1
            Else
            total_comp = total_comp + 1
                    If cell(component).Interior.ColorIndex = 3 Then
                    cell(component).Interior.ColorIndex = 4
                    cell(component).Interior.Pattern = xlLightVertical
                    Else: cell(component).Interior.ColorIndex = 3
                    cell(component).Interior.Pattern = xlLightVertical
                    End If
            End If
        Next cell
        cell("C2") = total_comp
    End Sub
    Not all forums are the same - seek and you shall find

  4. #4
    Registered User
    Join Date
    07-22-2011
    Location
    Eindhoven, Hollan
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Count and Highlight text cells

    Thanks Mordred and Simon!

+ 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