I have got this code:
With .Cells(i, 4)
If .Value > 0.8 Then
.Interior.Color = RGB(237, 67, 55) '<-- Red color
.Font.Color = vbWhite
ElseIf .Value > 0.6 Then
.Interior.Color = RGB(255, 190, 0) '<-- Amber Colour
.Font.Color = vbWhite
ElseIf .Value2 = "---" Then
.Interior.Color = .Interior.Color
Else
.Interior.Color = RGB(50, 205, 50) '<-- Green color
.Font.Color = vbWhite
End If
End With
The worksheet is already formatted to have alternating row colours by default but pending on the values of the cell the interior cell colour needs to change pending on the value of the cell but, some cells don't have numerical data and just "---". When this is the case I want the original formatting to remain as it was before the code was run.
Basically, If the cell contains "---" don't assign any other interior colour, but keep the colour already assigned.
I tested this and it seemed to work on my test data, for some reason excel is treating "---" as a value larger than 0.8 so moving the check for "---" first will stop this.
With .Cells(i, 4)
If .Value = "---" Then
.Interior.Color = .Interior.Color
ElseIf .Value > 0.8 Then
.Interior.Color = RGB(237, 67, 55) '<-- Red color
.Font.Color = vbWhite
ElseIf .Value > 0.6 Then
.Interior.Color = RGB(255, 190, 0) '<-- Amber Colour
.Font.Color = vbWhite
Else
.Interior.Color = RGB(50, 205, 50) '<-- Green color
.Font.Color = vbWhite
End If
End With
Related
So I have the following code that I use for comparing values in two columns, but it is based on if the value is in either column, NOT a side by side comparison.
Is there a way to easily modify the code to do a side by side comparison? (meaning I want it to check if cell A1=B1, A2=B2, A3=B3, etc)
Sub compare_cols()
'Get the last row
Dim Report As Worksheet
Dim i As Integer, j As Integer
Dim lastRow As Integer
Set Report = Excel.ActiveSheet
lastRow = Report.UsedRange.Rows.Count
Application.ScreenUpdating = False
For i = 2 To lastRow
For j = 2 To lastRow
If Report.Cells(i, 1).Value <> "" Then 'This will omit blank cells at the end (in the event that the column lengths are not equal.
If InStr(1, Report.Cells(j, 2).Value, Report.Cells(i, 1).Value, vbTextCompare) > 0 Then
Report.Cells(i, 1).Interior.Color = RGB(255, 255, 255) 'White background
Report.Cells(i, 1).Font.Color = RGB(0, 0, 0) 'Black font color
Exit For
Else
Report.Cells(i, 1).Interior.Color = RGB(156, 0, 6) 'Dark red background
Report.Cells(i, 1).Font.Color = RGB(255, 199, 206) 'Light red font color
End If
End If
Next j
Next i
'Now I use the same code for the second column, and just switch the column numbers.
For i = 2 To lastRow
For j = 2 To lastRow
If Report.Cells(i, 2).Value <> "" Then
If InStr(1, Report.Cells(j, 1).Value, Report.Cells(i, 2).Value, vbTextCompare) > 0 Then
Report.Cells(i, 2).Interior.Color = RGB(255, 255, 255) 'White background
Report.Cells(i, 2).Font.Color = RGB(0, 0, 0) 'Black font color
Exit For
Else
Report.Cells(i, 2).Interior.Color = RGB(156, 0, 6) 'Dark red background
Report.Cells(i, 2).Font.Color = RGB(255, 199, 206) 'Light red font color
End If
End If
Next j
Next i
Application.ScreenUpdating = True
End Sub
I am new to VBA and was learning it via doing exercises, one of the first macros was to color a box red If I click on a macro button. I recorded a macro initially to check whats the VBA code it uses to do that
Sub MakeMeRed()
'
' MakeMeRed Macro
'
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 192
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End Sub
I googled around found that there is another method to select active cell, so I was trying that method to fill the cell.
Sub TestMacro()
'
' TestMacro Macro
'
With ActiveCell
'.Value = "250"
.Color = 200
End With
End Sub
But this code does not work, it does not fill the color of selected cell. Can you point out where I'm going wrong?
Here are 2 ways to refer to the color, inside the cell.
Using the ActiveCell.Interior.ColorIndex and ActiveCell.Interior.Color. ActiveCell.Interior.Color can get 4 different values.
Sub TestMacro()
With ActiveCell
.Value = 1
.Interior.ColorIndex = 3
.Offset(1, 1) = 21
.Offset(1, 1).Interior.Color = RGB(255, 0, 0)
.Offset(1, 2) = 22
.Offset(1, 2).Interior.Color = vbRed 'vbRed = 255
.Offset(1, 3) = 23
.Offset(1, 3).Interior.Color = "&HFF" 'FF = 255; &H is for typeinfo
.Offset(1, 4) = 24
.Offset(1, 4).Interior.Color = 255
End With
End Sub
It looks like this:
To equate the two, ActiveCell in the section is the Selection in the first. To change the color, you'd need to slide in that Interior portion somewhere. You can either have With ActiveCell.Interior or within the with block .Interior.Color = 200.
i have got this code:
With .Cells(i, 6)
If .NumberFormat <> "0.0%" Then
.NumberFormat = "0.0%"
If .Value2 <> vbNullString And IsNumeric(.Value2) Then .Value = .Value / 100
If .Value2 = vbNullString Then
.Value = "---"
.HorizontalAlignment = xlRight
End If
If .Value >= 0.9 Then
.Interior.Color = RGB(237, 67, 55)
.Font.Color = vbWhite
End If
Else
.Value = 0
End If
End With
what the code does is look for values in a column that are more than 90% and if so it formats the cell interior to red and the font to white. but i have got some cells on the same column that do not have any value and hence i wanted the cell to have this "---" in it to make it look tidy but when i run the code the cells with "---" in it get formated to red fill and white font as well.
what i want is for those cells to remain with their original formatting.
i have written this if statement but dont know what to write after the "THEN" part:
IF .Value = "---" Then
i am a rookie! thanks for your help!
would this answer?
With .Cells(i, 6)
If .NumberFormat <> "0.0%" Then
.NumberFormat = "0.0%"
If .Value <> vbNullString And IsNumeric(.Value2) Then
.Value = .Value / 100
if .Value >= 0.9 Then
.Interior.Color = RGB(237, 67, 55)
.Font.Color = vbWhite
End If
elseIf .Value = vbNullString Then
.Value = "---"
.HorizontalAlignment = xlRight
Else
stop 'I think you forgot this case
end if
Else
.Value = 0
End If
End With
Edited: I add the suggestion of #Zac : use conditional formatting for the red color.
Prioritize your tests, this should do the trick :
With .Cells(i, 6)
If .NumberFormat <> "0.0%" Then
.NumberFormat = "0.0%"
If .Value2 <> vbNullString Then
If IsNumeric(.Value2) Then .Value2 = .Value2 / 100
If .Value2 >= 0.9 And .Value <> "---" Then
.Interior.Color = RGB(237, 67, 55)
.Font.Color = vbWhite
End If
Else
.Value = "---"
.HorizontalAlignment = xlRight
End If
Else
.Value = 0
End If
End With
I have got a list of percentage values that are really small (0.000% format), this represents the error percentage of routers. i want to format the cell color depending on the amount on the cell. if the amount is more than 0.050% it should be Red, if it is more than 0.005% is amber, everything else is green
here is the code that i have written:
With .Cells(i, 8)
If .NumberFormat <> "0.000%" Then
.NumberFormat = "0.000%"
If .Value2 <> vbNullString And IsNumeric(.Value2) Then .Value = .Value / 100
If .Value2 = vbNullString Then
.Value = "---"
.HorizontalAlignment = xlRight
End If
Else
.Value = 0
End If
If .Value > 0.05 Then
.Interior.Color = RGB(237, 67, 55) '<-- Red color
.Font.Color = vbWhite
ElseIf .Value > 0.005 Then
.Interior.Color = RGB(255, 190, 0) '<-- Amber Colour
.Font.Color = vbWhite
Else
.Interior.Color = RGB(50, 205, 50) '<-- Green color
.Font.Color = vbWhite
End If
End With
but the colour format is not accurate, here is the list of some of the results:
0.034% <---green
0.845% <---amber
0.007% <---green
0.005% <---green
0.094% <---green
it should not be like that as the cell that contains 0.845% and is amber should be bright red!
The value stored is not a percentage. It is the decimal equivalent, meaning you must shift the decimal point two places to the left. So to compare 0.05% you must use 0.0005.
This should clean up the code for you to make it a bit faster too:
Sub Test()
Dim Cel As Range, Rng As Range
Set Rng = Range("H1:H" & Range("H1048576").End(xlUp).Row).SpecialCells(xlCellTypeConstants)
For Each Cel In Rng
If Trim(Cel.Value) = "" Then Cel.Value = "---": Cel.HorizontalAlignment = xlRight
If IsNumeric(Cel.Value) Then
Cel.Value = Cel.Value / 100
If Cel.Value > 0.0005 Then
Cel.Interior.Color = RGB(237, 67, 55): Cel.Font.Color = vbWhite
ElseIf Cel.Value > 0.00005 Then
Cel.Interior.Color = RGB(255, 190, 0): Cel.Font.Color = vbWhite
Else: Cel.Interior.Color = RGB(50, 205, 50): Cel.Font.Color = vbWhite
End If
End If
Next
With Range("H1:H" & Range("H1048576").End(xlUp).Row).SpecialCells(xlCellTypeBlanks)
.Value = "---"
.HorizontalAlignment = xlRight
End With
End Sub
I'm just after realizing Paul corrected your question....
I have this VBA code that I am using to compare two columns in my Excel spreadsheet, column B to column A. It then "highlights" the ones that are missing from column A but in column B.
What I can't figure out is how to reverse the procedure to search column B and highlight the ones in column A that are different.
Original Code:
For i = 2 To LastRow
For j = 2 To LastRow
If Report.Cells(i, 2).Value <> "" Then 'This will omit blank cells at the end (in the event that the column lengths are not equal.
If InStr(1, Report.Cells(j, 1).Value, Report.Cells(i, 2).Value, vbTextCompare) > 0 Then
Report.Cells(i, 2).Interior.Color = xlNone 'Transparent background
Report.Cells(i, 2).Font.Color = RGB(0, 0, 0) 'Black font color
Exit For
Else
Report.Cells(i, 2).Interior.Color = RGB(156, 0, 6) 'Dark red background
Report.Cells(i, 2).Font.Color = RGB(255, 199, 206) 'Light red font color
End If
End If
Next j
Next i
I have tried renaming the letters and switching the column values and got close but realized that it was using the values from the original search and just highlighting the corresponding cells in column A.
To answer your question:
For j = 2 To LastRow
For i = 2 To LastRow
If Report.Cells(j, 1).Value <> "" Then 'This will omit blank cells at the end (in the event that the column lengths are not equal.
If InStr(1, Report.Cells(i, 2).Value, Report.Cells(j, 1).Value, vbTextCompare) > 0 Then
Report.Cells(j, 1).Interior.Color = xlNone 'Transparent background
Report.Cells(j, 1).Font.Color = RGB(0, 0, 0) 'Black font color
Exit For
Else
Report.Cells(j, 1).Interior.Color = RGB(156, 0, 6) 'Dark red background
Report.Cells(j, 1).Font.Color = RGB(255, 199, 206) 'Light red font color
End If
End If
Next i
Next j
If you wanted to used conditional formatting which makes the color changes live you can replace both loop with:
With Report.Range("A2:A" & LastRow).FormatConditions
.Delete
With .Add(Type:=xlExpression, Formula1:="=And(iserror(Vlookup(A2,B:B,1,False)),A2<>"""")")
.Font.Color = RGB(255, 199, 206)
.Interior.Color = RGB(156, 0, 6)
End With
End With
With Report.Range("B2:B" & LastRow).FormatConditions
.Delete
With .Add(Type:=xlExpression, Formula1:="=And(iserror(Vlookup(B2,A:A,1,False)),B2<>"""")")
.Font.Color = RGB(255, 199, 206)
.Interior.Color = RGB(156, 0, 6)
End With
End With
Edit the issue was that the data in Column A had an extra space at the end thus making the instr to return false.
For j = 2 To LastRow
Report.Cells(j, 1).Value = Trim(Report.Cells(j, 1).Value)
For i = 2 To LastRow
Report.Cells(i, 2).Value = Trim(Report.Cells(i, 2).Value)
If Report.Cells(j, 1).Value <> "" Then 'This will omit blank cells at the end (in the event that the column lengths are not equal.
If InStr(1, Report.Cells(i, 2).Value, Report.Cells(j, 1).Value, vbTextCompare) > 0 Then
Report.Cells(j, 1).Interior.Color = xlNone 'Transparent background
Report.Cells(j, 1).Font.Color = RGB(0, 0, 0) 'Black font color
Exit For
Else
Report.Cells(j, 1).Interior.Color = RGB(156, 0, 6) 'Dark red background
Report.Cells(j, 1).Font.Color = RGB(255, 199, 206) 'Light red font color
End If
End If
Next i
Next j
By trimming the values, the instr returned true.
There are many ways to accomplish this.
You could use formulas, you could create dictionaries.
A Quick solution would be:
Dim stringCount As Integer
Dim myString As String
Dim col1Range As Range
Dim col2Range As Range
Set col1Range = Report.Range("A1")
Set col2Range = Report.Range("B1")
For i = 1 To LastRow
myString = col1Range.Offset(i).Value
If myString <> "" Then
stringCount = WorksheetFunction.CountIf(Range("B:B"), myString)
If (stringCount = 0) Then
col1Range.Offset(i).Interior.Color = RGB(156, 0, 6) 'Dark red background
col1Range.Offset(i).Font.Color = RGB(255, 199, 206) 'Light red font color
Else
col1Range.Offset(i).Interior.Color = xlNone 'Transparent background
col1Range.Offset(i).Font.Color = RGB(0, 0, 0) 'Black font color
End If
End If
Next i
For j = 1 To LastRow
myString = col2Range.Offset(j).Value
If myString <> "" Then
stringCount = WorksheetFunction.CountIf(Range("A:A"), myString)
If (stringCount = 0) Then
col2Range.Offset(j).Interior.Color = RGB(156, 0, 6) 'Dark red background
col2Range.Offset(j).Font.Color = RGB(255, 199, 206) 'Light red font color
Else
col2Range.Offset(j).Interior.Color = xlNone 'Transparent background
col2Range.Offset(j).Font.Color = RGB(0, 0, 0) 'Black font color
End If
End If
Next j