Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub ApplyCustomFormatting()
- Dim ws As Worksheet
- Set ws = ThisWorkbook.Sheets("Design") ' Change as needed
- Dim lastRow As Long
- lastRow = ws.Cells(ws.Rows.Count, "Y").End(xlUp).Row
- Dim i As Long
- Dim cell As Range
- Dim fCrosshatch As String, fRed As String, fYellow As String, fGreen As String
- For i = 14 To 300 ' Adjust start row as needed
- Select Case ws.Cells(i, "Y").Value
- Case "Checkbox", "Dropdown", "Input"
- Set cell = ws.Cells(i, "Z")
- cell.Style = "Normal Entry"
- ' Data Validation
- With cell.Validation
- Select Case ws.Cells(i, "Y").Value
- Case "Checkbox"
- .Delete
- .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
- Operator:=xlBetween, Formula1:="=TorF"
- .IgnoreBlank = True
- .InCellDropdown = False
- .InputMessage = "Use Checkbox"
- .ErrorMessage = "Use Checkbox"
- .ShowInput = True
- .ShowError = True
- cell.Style = "Normal Entry Checkbox"
- Case "Dropdown"
- .Delete
- .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
- Operator:=xlBetween, Formula1:="=INDIRECT(X" & i & ")"
- .IgnoreBlank = True
- .InCellDropdown = True
- .ErrorMessage = "Please use the dropdown"
- .ShowError = True
- End Select
- End With
- ' Cell Style
- ' Clear existing formatting
- cell.FormatConditions.Delete
- ' Add left border without removing others
- With cell.Borders(xlEdgeLeft)
- .LineStyle = xlContinuous
- .Weight = xlThin
- .ColorIndex = xlAutomatic
- End With
- ' Shared conditions
- fCrosshatch = "=OR($V" & i & "=FALSE, AND($S" & i & "=FALSE, ReportType<>""Final""), AND($T" & i & "=FALSE, ReportType<>""Rough""))"
- fRed = "=OR($W" & i & "=TRUE, AND($Z" & i & "=TRUE, $V" & i & "=FALSE))"
- fYellow = "=AND($U" & i & ", NOT($Z" & i & "))"
- fGreen = "=$Z" & i & "=TRUE"
- Select Case ws.Cells(i, "Y").Value
- Case "Input"
- fGreen = "=LEN($Z" & i & ")>0"
- fRed = "=OR($W" & i & "=TRUE, AND(LEN(TRIM($Z" & i & "))>0, $V" & i & "=FALSE))"
- Case "Dropdown"
- fYellow = "=AND($U" & i & ", OR(LEN($Z" & i & ")=0, $Z" & i & "=""Not Met""))"
- fGreen = "=LEN($Z" & i & ")>0"
- fRed = "=OR($W" & i & "=TRUE, AND(LEN(TRIM($Z" & i & "))>0, $V" & i & "=FALSE))"
- End Select
- ' Apply Conditional Formatting
- ' 1. Crosshatch (NO stop if true)
- With cell.FormatConditions.Add(Type:=xlExpression, Formula1:=fCrosshatch)
- .Interior.Pattern = xlPatternChecker
- .Interior.PatternColorIndex = xlAutomatic
- .StopIfTrue = False
- End With
- ' 2. RED (shared across all types)
- With cell.FormatConditions.Add(Type:=xlExpression, Formula1:=fRed)
- if ws.Cells(i, "Y").Value = "Checkbox" Then
- .Font.Color = RGB(255, 0, 0)
- End If
- .Interior.Color = RGB(255, 0, 0)
- .StopIfTrue = True
- End With
- ' 3. YELLOW
- With cell.FormatConditions.Add(Type:=xlExpression, Formula1:=fYellow)
- if ws.Cells(i, "Y").Value = "Checkbox" Then
- .Font.Color = RGB(255, 255, 153)
- End If
- .Interior.Color = RGB(255, 255, 153)
- .StopIfTrue = True
- End With
- ' 4. GREEN
- With cell.FormatConditions.Add(Type:=xlExpression, Formula1:=fGreen)
- if ws.Cells(i, "Y").Value = "Checkbox" Then
- .Font.Color = RGB(146, 208, 80)
- End If
- .Interior.Color = RGB(146, 208, 80)
- .StopIfTrue = True
- End With
- End Select
- Next i
- MsgBox "Formatting applied to all rows in column Y."
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement