VBA Code: Command button To Change cell backgroundcolor if cell has specific value

Dedeke

Board Regular
Joined
Dec 1, 2020
Messages
70
Office Version
  1. 2016
Platform
  1. Windows
Hi there, newbie over here not a newbie in excel, but struggling VBA code. In different columns I have cells with a formula which result in a value "X". Some cells results in a number. Is it possible with a command button to change the background of only the cells marked with the value "x" and also for a specific range.
Ex: Ranges worksheet "A1: AB70" only change the color of the cells within the range "Q1: V70"
Already thinking of a second button to remove the colors again if necessary
Hopefully I explained well and did not made to many writing errors.

Already many thanks for even reading my question :)
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
You will need to change the affected range in the macro but here the first macro creates the color background. The second macro
returns the cell background color to normal.

VBA Code:
Option Explicit

Sub CellColorChange()
Dim MR, Cell As Range

Set MR = Range("G6:G12")
    For Each Cell In MR
        If Cell.Value > 5 Then
            Cell.Interior.ColorIndex = 15
        Else
            Cell.Interior.ColorIndex = 0
        End If
    Next
End Sub

Sub CellColorChange2()
Dim MR, Cell As Range

Set MR = Range("G6:G12")
    For Each Cell In MR
        If Cell.Interior.ColorIndex = 15 Then
            Cell.Interior.ColorIndex = 0
        End If
    Next
End Sub
 
Upvote 0
Solution
Logit,
Thx a lot for quick solution. Modified the range to my needs, and also changed the "If Cell.Value > 5" into "Cell.Value = "X" "
because the value X is the one i need in my case.
Tested imediatly: result : SUPER SMOOTH :) :) Gonna play arround with the ColorIndex nr. I suppose the colors gonna change depending the nrs I use.

Thx a lot..; (untill the next problem)
 
Upvote 0

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top