I have this code:
Sub CheckRevision()
Dim CurCell As Object
For Each CurCell In ActiveWorkbook.ActiveSheet.Range("B1:B5000")
If CurCell.Value = "Live" Then CurCell.Interior.Color = RGB(0, 204, 0)
Next
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("B1:B5000")) Is Nothing Then CheckRevision
End Sub
This works fine, however if I then have a cell which is "live" and change it back to "NOTLIVE" for example, the formatting is still a green cell. How do I get it to put it back to white default?
try: (but have a look art Peh's comment)
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("B1:B5000")) Is Nothing Then
For Each cl In Intersect(Target, Me.Range("B1:B5000"))
If UCase(cl.Value) = "LIVE" Then
cl.Interior.Color = RGB(0, 204, 0)
Else
cl.Interior.Color = xlNone
End If
Next
End If
End Sub
Just replace line If CurCell.Value = "Live" Then CurCell.Interior.Color = RGB(0, 204, 0)
with
If curcell.Value = "Live" Then
curcell.Interior.Color = RGB(0, 204, 0)
Else
curcell.Interior.Pattern = xlNone
End If
You don't really need the If statement at all.. you could just use this:
Sub CheckRevision()
Dim CurCell As Object
For Each CurCell In ActiveWorkbook.ActiveSheet.Range("B1:B5000")
CurCell.Interior.Color = xlNone - ((CurCell.Value = "Live") * (RGB(0, 204, 0) - xlNone))
Next
End Sub
So, how does this work? (Thanks, Pᴇʜ)
You're basically attempting to paint a cell with a colour that is either:
xlNone or RGB(0, 204, 0)
-4142 or 52224
This is decided by the CurCell.Value = "Live" which when used this way (cast into an integer) will return either 0 (for False) or -1 (for True).
Knowing that all this decision making results in a little bit of maths allows us to write an equation that causes the 0 or -1 to produce the two values:
If CurCell.Value = "Live" then the equation looks like this:
CurCell.Interior.Color
= xlNone - (-1 * (RGB(0, 204, 0) - xlNone))
... = xlNone - (-1 * (RGB(0, 204, 0) - xlNone))
... = RGB(0, 204, 0)
= 52224
If CurCell.Value <> "Live" then the equation looks like this:
CurCell.Interior.Color = xlNone - (0 * (RGB(0, 204, 0) - xlNone))
... = xlNone - (0 * (RGB(0, 204, 0) - xlNone))
... = xlNone
= -4142
if you aiming to change ONLY value of the cell you change the particular moment you can use the below which does not loop the whole range BUT test only the specific cell.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
'Check if the cell changed included in the range B1:B5000 & if only one cell changed to avoid errors
If Not Intersect(Target, Range("B1:B5000")) Is Nothing And Target.Count = 1 Then
'Call the module to apply formatting passing 3 parameters.
Call Module1.CheckRevision(Target.Worksheet, Target.Value, Target.Address)
End If
End Sub
Sub CheckRevision(wsName As Worksheet, cellValue As String, cellAddress As String)
With wsName.Range(cellAddress)
If cellValue = "Live" Then
.Interior.Color = RGB(0, 204, 0)
Else
.Interior.Pattern = xlNone
End If
End With
End Sub
Related
Below code is giving me an error stating specified value is out of range.
Sub Green()
ThisWorkbook.Sheets(1).Activate
ActiveSheet.Shapes("Elbow Connector 62").Fill.ForeColor.RGB = vbGreen
End Sub
Here's two options - one with a variable and one without. You might need to change the 2 to a 62.
Sub Macro1()
ActiveSheet.shapes.Range(Array("Elbow Connector 2")).Select
With Selection.ShapeRange.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 0, 0)
.Transparency = 0
End With
End Sub
Sub shapes()
Dim s As ShapeRange
Set s = ActiveSheet.shapes.Range(Array("elbow connector 2"))
With s
.Line.ForeColor.RGB = RGB(255, 0, 0)
End With
End Sub
If I have a column of referenced numbers and I would like to color the referenced cell automatically after right clicking, how can I refer to the reference cell in VBA. In the exmaple I have the referenced values in column [O]. At [O4] I have the value [=$G$12]. If I click on [O4] I want to color it together with the reference cell G12. It would be an additional step that I would like to color the cell above [G12] as well.
I tried to insert this in the formula:
REPLACE(SUBSTITUTE(FORMULATEXT($O$4),"$",""),1,1,"")
Which would give back the reference cell [G12] in a normal excel sheet without equal sign but the script doesn't work with it.
Thank you for the support in advance!
Select Case Target.Address
Case "$O$4" '<~ if cell A1 is clicked, highlight cells C5-C9 yellow
Cancel = True
If .Range("O4,G11:G12").Interior.Color = RGB(255, 153, 0) Then
.Range("O4,G11:G12").Interior.Color = RGB(255, 255, 255)
.Range("O4,G11:G12").Font.Bold = False
Else: .Range("O4,G11:G12").Interior.Color = RGB(255, 153, 0)
.Range("O4,G11:G12").Font.Bold = True
End If
End Select
you could use Precedents property of Range obejct:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Select Case Target.Address
Case "$O$4"
Cancel = True
Dim rng As Range
Set rng = Target.Precedents
If rng Is Nothing Then
Set rng = Target
Else
Set rng = Union(Target, rng, rng.Offset(-1))
End If
If rng.Interior.Color = RGB(255, 153, 0) Then
rng.Interior.Color = RGB(255, 255, 255)
rng.Font.Bold = False
Else
rng.Interior.Color = RGB(255, 153, 0)
rng.Font.Bold = True
End If
End Select
End Sub
Try this approach, please:
Dim strForm As String, newRng As Range
Select Case Target.Address
Case "$O$4" '<~ if cell A1 is clicked, highlight cells C5-C9 yellow
Cancel = True
strForm = Target.Formula
'split string formula on "$" character and use the array obtained elements, to build the new address
Set newRng = Range(Range(Split(strForm, "$")(1) & Split(strForm, "$")(2) - 1).Address & ":" & _
Range(Split(strForm, "$")(1) & Split(strForm, "$")(2)).Address)
If Range(Target.Address(0, 0) & "," & newRng.Address).Interior.Color = RGB(255, 153, 0) Then
Range(Target.Address(0, 0) & "," & newRng.Address).Interior.Color = RGB(255, 255, 255)
Range(Target.Address(0, 0) & "," & newRng.Address).Font.Bold = False
Else: Range(Target.Address(0, 0) & "," & newRng.Address).Interior.Color = RGB(255, 153, 0)
Range(Target.Address(0, 0) & "," & newRng.Address).Font.Bold = True
End If
End Select
Thank you very much for both of you. Finaly it works. I have an additonal qiestion in order to reduce the length of the code.
Lets consider it as a block for coloring cells if I clcik on [O4] cell. If I dont want to repeate this block many times but I want the same feature for the range of [O4:O45] with corresponding refernce content how I can do that? Sorry I'm not so experenced in coding but I'm very satisfied wth the result so far.
Thanks a lot!
I borrowed the code below from a post on this site a few years ago by Paul Bica.
(Using Conditional Formatting with Icon Sets with six conditions)
Option Explicit
Public Sub testIcons()
Application.ScreenUpdating = True
setIcon Sheet1.UsedRange
Application.ScreenUpdating = True
End Sub
Public Sub setIcon(ByRef rng As Range)
Dim cel As Range, sh As Shape, adr As String
For Each sh In rng.Parent.Shapes
If InStrB(sh.Name, "$") > 0 Then sh.Delete
Next: DoEvents
For Each cel In rng
If Not IsError(cel.Value2) Then
If Val(cel.Value2) > 0 And Not IsDate(cel) Then
adr = cel.Address
Set sh = Sheet1.Shapes.AddShape(msoShapeOval, cel.Left + 5, cel.Top + 2, 10, 10)
sh.ShapeStyle = msoShapeStylePreset38: sh.Name = adr
sh.Fill.ForeColor.RGB = getCelColor(Val(cel.Value2))
sh.Fill.Solid
End If
End If
Next
End Sub
Public Function getCelColor(ByRef celVal As Long) As Long
Select Case True
Case celVal = 1: getCelColor = RGB(211, 211, 211): Exit Function
Case celVal = 2: getCelColor = RGB(0, 0, 0): Exit Function
Case celVal = 3: getCelColor = RGB(255, 0, 0): Exit Function
Case celVal = 4: getCelColor = RGB(255, 153, 204): Exit Function
Case celVal = 5: getCelColor = RGB(255, 255, 51): Exit Function
Case celVal = 6: getCelColor = RGB(0, 204, 0): Exit Function
End Select
End Function
I would like to have it show the icons only without the value of the cell.
If you're okay with the number format of the cells being changed, I think after this line:
sh.Fill.Solid
you can put:
cel.NumberFormat = ";;;"
and that should effectively make the cell's content transparent1 (match the cell's background colour).
Seems to work for me:
1 Doesn't seem to work for cells containing errors (e.g. #N/A), but I think this is a non-issue as your code ignores error-containing cells.
I need two actions to take place on one sheet in my workbook. Both are based on a change event, but do not know how to make them both work. Below is the codes that I have:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range
Set r = Target.EntireRow
If Target.row = 1 Then Exit Sub ' Don't change header color
If r.Cells(1, "AD").Value <> "" Then
r.Font.Color = RGB(0, 176, 80)
Else
r.Font.ColorIndex = 1
End If
End Sub
And this one:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range
Set r = Target.EntireRow
If Target.row = 1 Then Exit Sub ' Don't change header color
If r.Cells(1, "E").Value = "6" Then
r.Font.Color = RGB(255, 0, 0)
Else
r.Font.ColorIndex = 1
End If
End Sub
To help determine the best course of action, here is what the end results must be:
For any row that has a date entered into cell AD, the text color for the entire row should change to green. However, if cell E of any row contains a 6 (this is a number formatted as text), then the text in that row should be red.
I am sure that I am over thinking this. All suggestions are appreciated.
Use an And in your first If statement, and add an ElseIf statements.
I am not exactly sure what you want to take precedence if both a date and 6 exist or if there is one without the other, but you can easily adjust the If Then ElseIf block below to sort out your needs.
If r.Cells(1, "AD").Value <> "" And r.cells(1,"E").Value = "6" Then
r.Font.Color = RGB(255, 0, 0)
ElseIf r.Cells(1,"AD").Value <> "" Then
r.Font.Color = RGB(0, 176, 80)
Else
r.Font.ColorIndex = 1
End If
I want to compare 2 columns from 2 different worksheets (The same column in each sheet), then compare if they have increased or decreased, then color the cell red or green to indicate if increased or decreased. However my formula doesn't work...
I tried the columns on the same sheet using activecell, and activecell offset and it worked, but I cant seem to reference it in another sheet?
But is my (unworking) code...
Sub test3()
Range("A1").Select
Do
If ActiveWorkbook.worksheets(“Sheet1!”).Cell(ActiveCell) > ActiveWorkbook.worksheets(“Sheet2!”).Cell(“A1”) Then
ActiveCell.Interior.Color = RGB(255, 0, 0)
ElseIf ActiveWorkbook.worksheets(“Sheet1!”).Cell(ActiveCell) < ActiveWorkbook.worksheets(“Sheet2!”).Cell(“A1”) Then
ActiveCell.Interior.Color = RGB(0, 255, 0)
Else
End If
ActiveCell.Offset(1, 0).Select
Loop Until IsEmpty(ActiveCell)
End Sub
Try this, just need to set column to whatever column index you need.
Dim row As Integer
Dim column As Integer
row = 1
column = 1
ActiveWorkbook.Worksheets("Sheet1!").Cells.Interior.Color = RGB(255, 255, 255)
Do While ActiveWorkbook.Worksheets("Sheet1!").Cells(row, column) <> ""
If ActiveWorkbook.Worksheets("Sheet1!").Cells(row, column) <> ActiveWorkbook.Worksheets("Sheet2!").Cells(row, column) Then
If ActiveWorkbook.Worksheets("Sheet1!").Cells(row, column) > ActiveWorkbook.Worksheets("Sheet2!").Cells(row, column) Then
ActiveWorkbook.Worksheets("Sheet1!").Cells(row, column).Interior.Color = RGB(255, 0, 0)
Else
ActiveWorkbook.Worksheets("Sheet1!").Cells(row, column).Interior.Color = RGB(0, 255, 0)
End If
End If
row = row + 1
Loop
This will work - just change the colorRng reference to suit your needs:
Sub ColorCode()
Dim cl As Range, colorRng As Range
Set colorRng = Worksheets(1).Range("A1:A10")
For Each cl In colorRng
If cl > Worksheets(2).Cells(cl.Row, cl.Column) Then
cl.Interior.Color = RGB(255, 0, 0)
Else
cl.Interior.Color = RGB(0, 255, 0)
End If
Next cl
End Sub