+ Reply to Thread
Results 1 to 4 of 4

VBA Code for Highlighting Cells based on If then Rules

Hybrid View

  1. #1
    Registered User
    Join Date
    11-19-2022
    Location
    USA
    MS-Off Ver
    365
    Posts
    2

    VBA Code for Highlighting Cells based on If then Rules

    Hi, Attached is the excel data file for which I need to create a visual basic code with the following rules.
    1. If AssetCondition=Good and ReviseRul=0 and RULCalculated<=10, highlight all 3 cells.
    2. If AssetCondition=Good and ReviseRul=1 and RULOverride<=10, highlight all 3 cells.
    3. If Priority=Priority 1 and Level 5 does not include any of the following words=UPS, Emergency, Fire, Annunciation, Generator, Sprinkler, highlight both cells.
    4. If RULCalculated!=YearinService+EUL-Current Year, highlight all 3 cells.
    5. IfUnitCost field is blank, then highlight that cell.
    6. If Manufacturer field is blank, then input "Not Visible" in cell
    7. If CapacityUnitOfMeasure=numeric value, highlight cell.
    8. Update all cells in PlanType column as capital letters.
    9. If YearManufactured contains comma then highlight cell.


    Can someone please help me write this rule. Thanks in advance
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    12-01-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2021
    Posts
    979

    Re: VBA Code for Highlighting Cells based on If then Rules

    try

    edited some values to test rules

    i think someone can think of a better code in lines of code in '3 :D

    Option Explicit
    
    Sub highlight_cells()
    
    Dim lRow As Long, i
    
    lRow = Cells(Rows.Count, 1).End(xlUp).Row ' last row in column A
    
    Range("A2:BH" & lRow).Interior.Color = xlNone
    
    'AssetCondition = P
    'ReviseRUL = AP
    'RULCalculated = AQ
    'RULOverride = AR
    'Priority = R
    'Level5 = J
    'YearinService = T
    'EUL = AT
    'UnitCost = L
    'Manufacturer = Y
    'CapacityUnitOfMeasure = X
    'PlanType = Q
    'YearManufactured = AJ
    
    For i = 2 To lRow
    '1
    If InStr(Cells(i, "P"), "Good") And Cells(i, "AP") = 0 And Cells(i, "AQ") <= 10 Then
        Range("P" & i & "," & "AP" & i & "," & "AQ" & i).Interior.ColorIndex = 33
    End If
    '2
    If InStr(Cells(i, "P"), "Good") And Cells(i, "AP") = 1 And Cells(i, "AR") <= 10 Then
        Range("P" & i & "," & "AP" & i & "," & "AR" & i).Interior.ColorIndex = 36
    End If
    '3
    If InStr(Cells(i, "R"), "Priority 1") Then
             If Not InStr(Cells(i, "J"), "UPS") Then
                If Not InStr(Cells(i, "J"), "Emergency") Then
                    If Not InStr(Cells(i, "J"), "Fire") Then
                        If Not InStr(Cells(i, "J"), "Annunciation") Then
                             If Not InStr(Cells(i, "J"), "Generator") Then
                                If Not InStr(Cells(i, "J"), "Sprinkler") Then
                                    Range("R" & i & "," & "J" & i).Interior.ColorIndex = 40
                                End If
                             End If
                        End If
                    End If
                End If
             End If
    End If
    '4
    If Cells(i, "T") = "" Then GoTo Five 'if blank assuming the rows column AQ and AT are blank also
    If IsNumeric(Cells(i, "AQ")) Then Cells(i, "AQ") = Cells(i, "AQ") * 1 'converted to num since stored as text
    If IsNumeric(Cells(i, "T")) Then Cells(i, "T") = Cells(i, "T") * 1 'converted to num since stored as text
    If IsNumeric(Cells(i, "AT")) Then Cells(i, "AT") = Cells(i, "AT") * 1 'converted to num since stored as text
    If Cells(i, "AQ") <> Cells(i, "T") + Cells(i, "AT") - Year(Date) Then
        Range("AQ" & i & "," & "T" & i & "," & "AT" & i).Interior.ColorIndex = 43
    End If
    '5
    Five:
    If Cells(i, "L") = "" Then Cells(i, "L").Interior.ColorIndex = 4
    '6
    If Cells(i, "Y") = "" Then Cells(i, "Y") = "Not Visible"
    '7
    If IsNumeric(Cells(i, "X")) Then Cells(i, "X").Interior.ColorIndex = 44
    '8
    Cells(i, "Q") = UCase(Cells(i, "Q"))
    '9
    If InStr(Cells(i, "AJ"), ",") Then Cells(i, "AJ").Interior.ColorIndex = 50
    
    Next i
    
    End Sub
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    11-19-2022
    Location
    USA
    MS-Off Ver
    365
    Posts
    2

    Re: VBA Code for Highlighting Cells based on If then Rules

    Thank you soo much, I will try this out. The rules I posted below are sample, I have about 50 such rules, would you know if I can continue coding the remaining 40 similar to how you have done for the below 9 or is there a max limit?

  4. #4
    Valued Forum Contributor
    Join Date
    12-01-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2021
    Posts
    979

    Re: VBA Code for Highlighting Cells based on If then Rules

    im not sure about 50 rules in a single sub is not ideal
    maybe breakdown each rule in each sub maybe

+ 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. excel help calander (highlighting/rules) need new formula
    By crashins in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-28-2017, 11:18 PM
  2. Code/Rules for pulling cells between tabs
    By cuttothechase in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-28-2017, 10:56 AM
  3. Help With Creating a VBA code based on 4 rules
    By anfdrew in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-09-2014, 08:26 PM
  4. Excel VBA code to sort table and move cells around according to set rules
    By abdul4me in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-22-2013, 07:18 AM
  5. Need a vba code to check the status and based on rules calculate service years
    By baba_excel in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-19-2012, 09:03 PM
  6. Sum cells based on 2 variable rules
    By GenericPat in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 10-23-2012, 09:56 AM
  7. Replies: 2
    Last Post: 10-08-2008, 04:35 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