Tuesday, 14 July 2026

VBA Macro To Fill Cells Automatically

 Sub ApplyHexColors()

    Dim cell As Range

    Dim hexVal As String

    Dim r As Integer, g As Integer, b As Integer

    

    On Error Resume Next

    For Each cell In Selection

        ' Strip the "#" if present

        hexVal = Replace(cell.Value, "#", "")

        

        If Len(hexVal) = 6 Then

            ' Convert Hex to RGB

            r = CInt("&H" & Mid(hexVal, 1, 2))

            g = CInt("&H" & Mid(hexVal, 3, 2))

            b = CInt("&H" & Mid(hexVal, 5, 2))

            

            ' Apply as background fill

            cell.Interior.Color = RGB(r, g, b)

        End If

    Next cell

End Sub