• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Macro to change color of the cell of specific column

ashish mehra

Active Member
Hi,

I want a code that will change the color of the 'Sales Date" column depeding on the condition.

Condition:
if the Sales Date is less than 2 years fill the cell with Green,
if between 2 - 5 years fill the cell with Yellow and if more than 5 years than Red.

Regards,
AM
 

Attachments

  • Change color of the column.xlsx
    8.5 KB · Views: 4
Hi, ashish mehra!
Do you actually need VBA code for this? You can apply conditional format, check the uploaded file.
Regards!
 

Attachments

  • Change color of the column.xlsx
    9.5 KB · Views: 3
Definition of Open Book test...
Open book exams allow you to take notes, texts or resource materials into an examsituation. They test your ability to find and apply information and knowledge, so are often used in subjects requiring direct reference to written materials, like law statutes, statistics or acts of parliament.
 
Hi, ashish mehra!
Put this code where it goes, then run it.
Code:
Option Explicit

Sub ReinventingTheSquaredWheel()
    Dim I As Integer, J As Integer, K As Long
    '
    Range("A2:B12").Select
    With Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    '
    For I = 2 To 12
        K = Year(Now() - Cells(I, 1).Value) - 1900
        Select Case K
            Case Is <= 2
                Range("A" & I & ":B" & I).Select
                With Selection.Interior
                    .Pattern = xlSolid
                    .PatternColorIndex = xlAutomatic
                    .Color = 5296274
                    .TintAndShade = 0
                    .PatternTintAndShade = 0
                End With
            Case Is <= 5
                Range("A" & I & ":B" & I).Select
                With Selection.Interior
                    .Pattern = xlSolid
                    .PatternColorIndex = xlAutomatic
                    .Color = 65535
                    .TintAndShade = 0
                    .PatternTintAndShade = 0
                End With
            Case Else
                Range("A" & I & ":B" & I).Select
                With Selection.Interior
                    .Pattern = xlSolid
                    .PatternColorIndex = xlAutomatic
                    .Color = 255
                    .TintAndShade = 0
                    .PatternTintAndShade = 0
                End With
        End Select
    Next I
End Sub
Good auto cheat.
Regards!
 
Hi, ashish mehra!
Glad you solved it.
Thanks for your feedback but let me disagree. A proof? Read me at other threads started by you.
Welcome back whenever needed or wanted. And that doesn't mean "always before ever trying to solve a test".
Regards!
 
Back
Top