+ Reply to Thread
Results 1 to 5 of 5

Highlight rows multiple criteria vba

Hybrid View

realrookie Highlight rows multiple... 07-07-2016, 10:17 AM
6StringJazzer Re: Highlight rows multiple... 07-07-2016, 10:49 AM
realrookie Re: Highlight rows multiple... 07-07-2016, 11:08 AM
JOHN H. DAVIS Re: Highlight rows multiple... 07-07-2016, 03:29 PM
realrookie Re: Highlight rows multiple... 07-08-2016, 04:38 AM
  1. #1
    Forum Contributor
    Join Date
    07-14-2012
    Location
    Belgium/UK
    MS-Off Ver
    Excel 2003
    Posts
    111

    Highlight rows multiple criteria vba

    Hello,

    Looking for a code that highlights rows when criteria are met (or not) via a InputBox.

    e.g.: run macro, input box 1: enter premium, input box 2: enter selected. Run throw the rows and every row that has "premium" and "selected" higlights green, but if there is only 1 of the criteria in the row, highlight red.

    Attached is sample of the expected result.


    I tried changing the following code as it highlights cells but no luck.

    Sub HighlightValue()
    'PURPOSE: Highlight all cells containing a specified values
    Dim fnd As String, FirstFound As String
    Dim FoundCell As Range, rng As Range
    Dim myRange As Range, LastCell As Range
    'What value do you want to find?
      fnd = InputBox("First Value", "Find", "enter text")
         'End Macro if Cancel Button is Clicked or no Text is Entered
          If fnd = vbNullString Then Exit Sub
    Set myRange = ActiveSheet.UsedRange
    Set LastCell = myRange.Cells(myRange.Cells.Count)
    Set FoundCell = myRange.Find(what:=fnd, after:=LastCell)
    'Test to see if anything was found
      If Not FoundCell Is Nothing Then
        FirstFound = FoundCell.Address
      Else
        GoTo NothingFound
      End If
    Set rng = FoundCell
    'Loop until cycled through all unique finds
      Do Until FoundCell Is Nothing
        'Find next cell with fnd value
          Set FoundCell = myRange.FindNext(after:=FoundCell)
        'Add found cell to rng range variable
          Set rng = Union(rng, FoundCell)
        'Test to see if cycled through to first found cell
          If FoundCell.Address = FirstFound Then Exit Do
      Loop
    'Highlight Found cells yellow
      rng.Interior.Color = RGB(255, 255, 0)
    'Report Out Message
      MsgBox rng.Cells.Count & " cell(s) were found containing: " & fnd
    Exit Sub
    'Error Handler
    NothingFound:
      MsgBox "No cells containing:"
    End Sub
    Any help appreciated.

    Red
    Attached Files Attached Files

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,003

    Re: Highlight rows multiple criteria vba

    This code is a bit more straightforward, and it works. You will need to add code to accumulate the counts, if you need counts. You can also add error messages, if you want.

    Option Explicit
    
    Public Sub CheckRows()
    
       Dim LastRow As Long
       Dim Search1 As String, Search2 As String
       Dim R As Long
       
       LastRow = Cells(Rows.Count, "C").End(xlUp).Row
       
       Search1 = InputBox("Enter value for Product 1")
       If Search1 = "" Then Exit Sub
       
       Search2 = InputBox("Enter value for Product 2")
       If Search2 = "" Then Exit Sub
       
       For R = 3 To LastRow
          If Cells(R, "C") = Search1 And Cells(R, "D") = Search2 Then
             Cells(R, "C").EntireRow.Interior.Color = RGB(0, 255, 0)
          ElseIf Cells(R, "C") = Search1 Or Cells(R, "D") = Search2 Then
             Cells(R, "C").EntireRow.Interior.Color = RGB(255, 0, 0)
          End If
       Next R
       
    End Sub
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor
    Join Date
    07-14-2012
    Location
    Belgium/UK
    MS-Off Ver
    Excel 2003
    Posts
    111

    Re: Highlight rows multiple criteria vba

    Hello sir,
    thank you for the reply. Indeed it works on the sample sheet, but my mistake. I should have mentioned that the search of the Inputbox is not limited to a specific column, as this changes from client to client.
    After adding the code to live data, nothing actually happens (even after I changed the columns).

    Would it be dependent on case sensitive and hidden columns?

    Sample sheet with live cleaned data attached.
    Attached Files Attached Files
    Last edited by realrookie; 07-07-2016 at 11:47 AM.

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

    Re: Highlight rows multiple criteria vba

    Another:

    Sub realrookiezz()
    Dim x As Range, y As Range, i As Long, crit1 As String, crit2 As String
    crit1 = InputBox("Please Enter Criteria 1")
    crit2 = InputBox("Please Enter Criteria 2")
    For i = Range("A" & Rows.Count).End(3).row To 2 Step -1
        Set x = Rows(i).Find(crit1, LookIn:=xlValues, lookat:=xlPart)
        Set y = Rows(i).Find(crit2, LookIn:=xlValues, lookat:=xlPart)
        If x Is Nothing And y Is Nothing Then
            GoTo zz
        ElseIf Not x Is Nothing And Not y Is Nothing Then
            Rows(i).Interior.ColorIndex = 4
        Else
            Rows(i).Interior.ColorIndex = 3
        End If
    zz:
    Next i
    End Sub

  5. #5
    Forum Contributor
    Join Date
    07-14-2012
    Location
    Belgium/UK
    MS-Off Ver
    Excel 2003
    Posts
    111

    Re: Highlight rows multiple criteria vba

    Mr Davis,

    Wonderful as always. But this runs to good. It does what I ask, but when I added it to the live data, i got more results then I expected. Live data has around 28k rows of products and up to Column AP.

    Is there a way, so that I can tell it in which column to search first for Criteria 1, and based on those results it would search the rest of the row (where criteria 1 was found) for criteria 2.

+ 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 cell for a criteria within multiple rows
    By ameeto.nike in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 02-02-2016, 10:34 PM
  2. [SOLVED] Highlight Duplicate Rows Based on Multiple Criteria
    By freybe06 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-15-2013, 09:39 AM
  3. Replies: 1
    Last Post: 10-30-2012, 05:40 PM
  4. Replies: 7
    Last Post: 09-13-2011, 12:04 PM
  5. Highlight according to multiple criteria
    By Rage in forum Excel General
    Replies: 6
    Last Post: 10-04-2010, 07:11 AM
  6. Identify/highlight maximum value based on multiple criteria
    By JennB in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 12-06-2009, 06:28 PM
  7. [SOLVED] match multiple criteria and highlight corresponding row in Excel
    By D Assess in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-02-2006, 12:30 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