+ Reply to Thread
Results 1 to 9 of 9

Count cells in row based on cell value, highlight row if wrong number or cells.

Hybrid View

  1. #1
    Registered User
    Join Date
    04-29-2014
    Location
    Houston, Texas
    MS-Off Ver
    Excel 2013
    Posts
    47

    Count cells in row based on cell value, highlight row if wrong number or cells.

    I want to have a macro that checks the letters in column E and counts the cells in the row, and if the number does not match a preset for those letters combinations the row gets highlighted red.
    In the example attached, row 2 has BLD the column E, so that row should have a total of 7 columns filled out.
    So:
    CP needs 5 count
    BLD needs 7 count
    ED needs 6 count
    TBKR needs 0 count
    And so on…

    I will need to be able to add additional lines of code to the macro to check the more than 150 letter combinations.
    Any help would be appreciated.


    SampleRESULT.xlsxSample.xlsx
    Last edited by gutterball; 09-09-2014 at 04:25 PM.

  2. #2
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: Count cells in row based on cell value, highlight row if wrong number or cells.

    Well, it wouldn't be possible to have a 0 count for TBKR, so I adjusted that to 5. This should do it though.

    Option Explicit
    Dim Rw As Long, Cnt As Long, Preset As Long
    
    Sub CheckCount()
        Application.ScreenUpdating = False
        Cells.Interior.ColorIndex = xlAutomatic
        For Rw = 1 To Range("A" & Rows.Count).End(xlUp).Row
            Cnt = WorksheetFunction.CountA(Rows(Rw))
            Select Case Cells(Rw, 5).Value
                Case "TBKR": Preset = 5
                Case "CP":   Preset = 5
                Case "ED":   Preset = 6
                Case "BLD":  Preset = 7
                Case Else:   Preset = 0
            End Select
            If Cnt <> Preset Then
                If Preset > 0 Then
                    Rows(Rw).Interior.ColorIndex = 38
                End If
            End If
        Next
    End Sub
    Acts 4:12
    Salvation is found in no one else, for there is no other name under heaven given to mankind by which we must be saved.

  3. #3
    Registered User
    Join Date
    04-29-2014
    Location
    Houston, Texas
    MS-Off Ver
    Excel 2013
    Posts
    47

    Re: Count cells in row based on cell value, highlight row if wrong number or cells.

    Thank you so much for coding this for me. I hope others and I can learn from your experience.

  4. #4
    Registered User
    Join Date
    04-29-2014
    Location
    Houston, Texas
    MS-Off Ver
    Excel 2013
    Posts
    47

    Re: Count cells in row based on cell value, highlight row if wrong number or cells.

    Thank you for your hard work helping me with this code.

  5. #5
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 365 version 2501
    Posts
    18,869

    Re: Count cells in row based on cell value, highlight row if wrong number or cells.

    Nate beat me to it, and with a nice code too I might add, but since I had worked this up I thought I would reply anyway. It highlights the same two rows that you highlighted in the 'sample' spreadsheet, so I hope it will work on your entire set of data.

    Dim rw As Long
    
    Sub sample()
    For rw = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        Select Case UCase(Cells(rw, 5).Value)
            Case "BLD", "EOW", "INLSW"
                If Cells(rw, Columns.Count).End(xlToLeft).Column <> 7 Then
                    Range(Cells(rw, 1), Cells(rw, 20)).Interior.ColorIndex = 3
                End If
            Case "ED", "ER"
                If Cells(rw, Columns.Count).End(xlToLeft).Column <> 6 Then
                    Range(Cells(rw, 1), Cells(rw, 20)).Interior.ColorIndex = 3
                End If
            Case "CP", "TBKR", "CLW", "NG"
                If Cells(rw, Columns.Count).End(xlToLeft).Column <> 5 Then
                    Range(Cells(rw, 1), Cells(rw, 20)).Interior.ColorIndex = 3
                End If
            Case "XCLW"
                If Cells(rw, Columns.Count).End(xlToLeft).Column <> 9 Then
                    Range(Cells(rw, 1), Cells(rw, 20)).Interior.ColorIndex = 3
                End If
        End Select
    Next rw
    End Sub
    Christ's Peace and Best of Luck

  6. #6
    Registered User
    Join Date
    04-29-2014
    Location
    Houston, Texas
    MS-Off Ver
    Excel 2013
    Posts
    47

    Re: Count cells in row based on cell value, highlight row if wrong number or cells.

    Quote Originally Posted by JeteMc View Post
    Nate beat me to it, and with a nice code too I might add, but since I had worked this up I thought I would reply anyway. It highlights the same two rows that you highlighted in the 'sample' spreadsheet, so I hope it will work on your entire set of data.

    Dim rw As Long
    
    Sub sample()
    For rw = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        Select Case UCase(Cells(rw, 5).Value)
            Case "BLD", "EOW", "INLSW"
                If Cells(rw, Columns.Count).End(xlToLeft).Column <> 7 Then
                    Range(Cells(rw, 1), Cells(rw, 20)).Interior.ColorIndex = 3
                End If
            Case "ED", "ER"
                If Cells(rw, Columns.Count).End(xlToLeft).Column <> 6 Then
                    Range(Cells(rw, 1), Cells(rw, 20)).Interior.ColorIndex = 3
                End If
            Case "CP", "TBKR", "CLW", "NG"
                If Cells(rw, Columns.Count).End(xlToLeft).Column <> 5 Then
                    Range(Cells(rw, 1), Cells(rw, 20)).Interior.ColorIndex = 3
                End If
            Case "XCLW"
                If Cells(rw, Columns.Count).End(xlToLeft).Column <> 9 Then
                    Range(Cells(rw, 1), Cells(rw, 20)).Interior.ColorIndex = 3
                End If
        End Select
    Next rw
    End Sub
    Christ's Peace and Best of Luck
    Great work!

    Can only the highlighted fields be placed onto a second work sheet? Can I add this function to this code?

  7. #7
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 365 version 2501
    Posts
    18,869

    Re: Count cells in row based on cell value, highlight row if wrong number or cells.

    Gutterball, Thanks for the compliment, I am sorry that I do not have a ready answer for the question about placing the incomplete rows in a second work sheet, I can look into that, but it may take some time. I note that Nate has already added that functionality to his code, so perhaps that would be your better option.

  8. #8
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: Count cells in row based on cell value, highlight row if wrong number or cells.

    Option Explicit
    Dim Rw As Long, Cnt As Long, Preset As Long, Rw2 As Long
    
    Sub CheckCount()
        Application.ScreenUpdating = False
        Cells.Interior.ColorIndex = xlAutomatic
        For Rw = 1 To Range("A" & Rows.Count).End(xlUp).Row
            Cnt = WorksheetFunction.CountA(Rows(Rw))
            Select Case Cells(Rw, 5).Value
                Case "TBKR": Preset = 5
                Case "CP":   Preset = 5
                Case "ED":   Preset = 6
                Case "BLD":  Preset = 7
                Case Else:   Preset = 0
            End Select
            If Cnt <> Preset Then
                If Preset > 0 Then
                    Rows(Rw).Interior.ColorIndex = 38
                    With Sheets("Sheet2")
                        Rw2 = .Range("A" & .Rows.Count).End(xlUp).Row + 1
                        Rows(Rw).Copy Destination:=.Rows(Rw2)
                    End With
                End If
            End If
        Next
    End Sub

  9. #9
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 365 version 2501
    Posts
    18,869

    Re: Count cells in row based on cell value, highlight row if wrong number or cells.

    I added the functionality for copying and pasting the first 20 cells from each 'incomplete' row to a separate sheet.
    I have also added some comments to the code that may be useful in updating it should the need arise.

    Sub sample()
    Dim Rw As Long, markCopy As Boolean
    Sheets("SampleRESULT").Select        'this will start the code on the correct sheet
    markCopy = False
    For Rw = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        Select Case UCase(Cells(Rw, 5).Value)
            Case "BLD", "EOW", "INLSW"  'if you need to add another section of code, copy from here
                If Cells(Rw, Columns.Count).End(xlToLeft).Column <> 7 Then
                markCopy = True
                End If                  'to here and of course change the number after <>
            Case "ED", "ER" 'you can append any Case
                If Cells(Rw, Columns.Count).End(xlToLeft).Column <> 6 Then
                    markCopy = True
                End If
            Case "CP", "TBKR", "CLW", "NG"
                If Cells(Rw, Columns.Count).End(xlToLeft).Column <> 5 Then
                    markCopy = True
                End If
            Case "XCLW"
                If Cells(Rw, Columns.Count).End(xlToLeft).Column <> 9 Then
                    markCopy = True
                End If
                'if you add another section of code paste what you copied from above here
        End Select
        If markCopy = True Then
                Range(Cells(Rw, 1), Cells(Rw, 20)).Interior.ColorIndex = 3
                Range(Cells(Rw, 1), Cells(Rw, 20)).Select
                 Selection.Copy
                 Sheets("Sheet1").Select
                 Range("A" & Rows.Count).End(xlUp).Offset(1).Select 'starts pasting on row 2
                 Selection.PasteSpecial Paste:=xlPasteValues
                 Application.CutCopyMode = False
                 Sheets("SampleRESULT").Select
                 Range("A" & Rw).Select
                markCopy = False
        End If
    Next Rw
    End Sub
    Hope that this will be of some help.
    Christ's Peace and Best of Luck

+ 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. [SOLVED] highlight a series of cells based on the number of days in a date range
    By b2lynch in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-09-2014, 10:48 PM
  2. [SOLVED] Highlight Cells based on Cell Reference on another Worksheet
    By HangMan in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-28-2014, 08:24 AM
  3. highlight specific cells in row based on one cell value in same row.
    By mgbanc in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-22-2013, 06:58 AM
  4. Highlight 1 cell based on value from other cells
    By freeon in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 09-10-2013, 01:24 PM
  5. Highlight or colour a cell or cells based on cells in another range.
    By baffld in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 10-30-2012, 08:41 PM

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