Avatar

AI vba macro (Ogólne)

by smola ⌂, 2025-03-03, 06:22 (13 days ago) @ Neo

AI jednak na cos moze sie przydac, moja ostatnio polubiona wyszukiwarka perplexity.ai tak twierdzi ;)
Tutaj jej pelna odpowiedz a nizej kod jaki wyplula, wyglada na ok, ale musisz potestowac:
conditional formattinig as vba macro in Microsoft Office 95

Sub ApplyConditionalFormatting()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim lastCol As Long
    Dim colIndex As Integer
    Dim dataRange As Range
    Dim maxRow As Long

    ' Set the worksheet
    Set ws = ActiveSheet

    ' Find the last row and column with data
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column

    ' Start from column C (3rd column)
    For colIndex = 3 To lastCol
        ' Define the range for conditional formatting (2nd row to second-to-last row)
        Set dataRange = ws.Range(ws.Cells(2, colIndex), ws.Cells(lastRow - 1, colIndex))

        ' Clear any existing conditional formatting
        dataRange.FormatConditions.Delete

        ' Add a Color Scale conditional format (green to red)
        With dataRange.FormatConditions.AddColorScale(2)
            .ColorScaleCriteria(1).Type = xlConditionValueLowestValue
            .ColorScaleCriteria(1).FormatColor.Color = RGB(0, 255, 0) ' Green
            .ColorScaleCriteria(2).Type = xlConditionValueHighestValue
            .ColorScaleCriteria(2).FormatColor.Color = RGB(255, 0, 0) ' Red
        End With

        ' Add a formula to calculate the maximum value in the column at the bottom row
        maxRow = lastRow + 1
        ws.Cells(maxRow, colIndex).Formula = "=MAX(" & dataRange.Address & ")"
    Next colIndex

    MsgBox "Conditional formatting applied and maximum values calculated!"
End Sub


Complete thread: