0

I have the following code, which SET 1 Columns H:K respond to Columns F:G. SET 2 Columns N:Q respond to Columns L:M. Set 3 Columns T:W respond to Columns R:S. Now following that same logic I want the final column in each set to respond if F, L, or R are True or F&G, L&M, R,S are True then the final column in the set should remain blank and color fill white, but the last column should G,M,or S be True in any of the sets should calulate the first column (H,N,T) + 2nd to Last column (J,P,V) For example if G is True then H+J should show in K. Now also if using this example if '=IF(K >= AB1, TRUE, FALSE)' text in green, if 'K < AB1, TRUE, FALSE' text should show in Red and if '=AND(A40=TRUE, F=TRUE)' then text should be italic.

I need another Private Sub I believe but this is the current base that should stay mostly the same:

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Tracker")
    
    'Build range all at once by intersecting whole columns and rows
    Dim rng As Range             'Specify columns           'Specify rows
    Set rng = Intersect(ws.Range("F:G, L:M, R:S"), ws.Range("12:29, 35:79, 86:102, 110:150"))
    
    'Identify total changed cells
    Dim rChanged As Range: Set rChanged = Intersect(Target, rng)
    
    'Prepare loop vars
    Dim rChangedCell As Range, rArea As Range
    Dim rCell1 As Range, rCell2 As Range
    
    If Not rChanged Is Nothing Then
        Application.EnableEvents = False
        
        For Each rChangedCell In rChanged.Cells
            For Each rArea In rng.Areas
                Set rCell1 = Intersect(rArea, rChangedCell)
                If Not rCell1 Is Nothing Then
                    If rCell1.Column <> rArea.Column Then Set rCell1 = ws.Cells(rCell1.Row, rArea.Column)
                    Exit For
                End If
            Next rArea
            Set rCell2 = rCell1.Offset(, 1)
            Call UpdateLine(rCell1, rCell2)
        Next rChangedCell
        
        Application.EnableEvents = True
    End If

End Sub

Private Sub UpdateLine(ByVal p_rCell1 As Range, ByVal p_rCell2 As Range)

    Dim aResult As Variant
    
    Select Case (Abs(p_rCell1.Value = True) + Abs(p_rCell2.Value = True) * 2)
        '0 means both cells are False
        Case 0:     aResult = Array(False, False, vbNullString, vbNullString)
        
        '1 means p_rCell1 is True
        Case 1:     aResult = Array(True, False, Date, "No")
        
        'Otherwise p_rCell2 is True
        Case Else:  aResult = Array(False, True, Date, "Yes")
    End Select
    
    p_rCell1.Resize(, UBound(aResult) - LBound(aResult) + 1).Value = aResult

End Sub
1
  • =IF(K >= AB1, TRUE, FALSE) do you mean TRUE in green, FALSE in red. =AND(A40=TRUE, F=TRUE) ? what is F Commented Jun 17 at 19:31

1 Answer 1

0

Add the formatting code into Sub UpdateLine()

Private Sub UpdateLine(ByVal p_rCell1 As Range, ByVal p_rCell2 As Range)

    Dim aResult As Variant, bCalc As Boolean, v, rngEnd As Range
    
    Select Case (Abs(p_rCell1.Value = True) + Abs(p_rCell2.Value = True) * 2)
        '0 means both cells are False
        Case 0:     aResult = Array(False, False, vbNullString, vbNullString)
        
        '1 means p_rCell1 is True
        Case 1:     aResult = Array(True, False, Date, "No")
        
        ' p_rCell2 is True
        Case 2:  aResult = Array(False, True, Date, "Yes")
                 bCalc = True
        
        ' p_rCell1 is True and p_rCell2 is True
        Case 3:  aResult = Array(False, True, Date, "Yes")
                 
    End Select
    
    p_rCell1.Resize(, UBound(aResult) - LBound(aResult) + 1).Value = aResult
    
    ' formatting
    Set rngEnd = p_rCell2.Offset(, 4)
    If bCalc Then
        v = p_rCell2.Offset(, 1).Value + p_rCell2.Offset(, 3).Value
        If v >= Range("AB$1") Then
           rngEnd = True
           rngEnd.Interior.Color = vbGreen
        Else
           rngEnd = False
           rngEnd.Interior.Color = vbRed
        End If
        
        'rngEnd.Font.Italic = Range ("$A$40") And Range("$F") ??
    Else
        rngEnd.Value = vbNullString
        rngEnd.Interior.Color = vbWhite
        rngEnd.Font.Italic = False
    End If

End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.