I am writing a code for ping tester.
In sheet one it keeps on pinging devices continuously and displays the ping time in column B. When any device becomes unreachable it shows the last ping time and duration of unreachability in next column. But when that device becomes reachable it sends the duration of reachability (report) to next sheet and start showing that device reachable.
I want to open the report sheet while macro is running in sheet1.
If I'm using select (as in code) it forces me to sheet1 but without this if I open sheeet2 the pinging time started typing in sheet2.
Sub Do_ping()
With ActiveWorkbook.Worksheets(1)
Worksheets("sheet1").Select
row = 2
Do
If .Cells(row, 1) <> "" Then
If IsConnectible(.Cells(row, 1), 2, 100) = True Then
Worksheets("sheet1").Select
If Cells(row, 3).Value = nul Then
Cells(row, 1).Interior.Color = RGB(0, 255, 0)
Cells(row, 1).Font.FontStyle = "bold"
Cells(row, 1).Font.Size = 14
Cells(row, 2).Interior.Color = RGB(0, 255, 0)
Cells(row, 2).Value = Time
Else
Worksheets("sheet1").Select
Cells(row, 1).copy Sheets("sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
Cells(row, 2).copy Sheets("sheet2").Range("B" & Rows.Count).End(xlUp).Offset(1, 0)
Cells(row, 5).copy Sheets("sheet2").Range("c" & Rows.Count).End(xlUp).Offset(1, 0)
Cells(row, 1).Interior.Color = RGB(0, 255, 0)
Cells(row, 1).Font.FontStyle = "bold"
Cells(row, 1).Font.Size = 14
Cells(row, 2).Interior.Color = RGB(0, 255, 0)
Cells(row, 2).Value = Time
Cells(row, 5).ClearContents
End If
'Call siren
Else:
'Cells(Row, 2).Formula = "=NOW()-" & CDbl(Now())
'Cells(Row, 1).Interior.Color = RGB(255, 0, 0)
Worksheets("sheet1").Select
Cells(row, 3).Value = DateDiff("d", Cells(row, 2), Now())
'Time Difference. First set the format in cell.
Cells(row, 4).NumberFormat = "hh:mm:ss"
'/calculate and update
Cells(row, 4).Value2 = Now() - Cells(row, 2)
Cells(row, 5).Value = Hour(Cells(row, 4).Value2) * 3600 + Minute(Cells(row, 4).Value2) * 60 + Second(Cells(row, 4).Value2)
If Cells(row, 5).Value > 120 Then
Worksheets("sheet1").Select
Cells(row, 1).Interior.ColorIndex = 3
Cells(row, 2).Interior.ColorIndex = 3
Cells(row, 3).Interior.ColorIndex = 3
Cells(row, 4).Interior.ColorIndex = 3
Else
Worksheets("sheet1").Select
Cells(row, 1).Interior.ColorIndex = 40
Cells(row, 2).Interior.ColorIndex = 40
Cells(row, 3).Interior.ColorIndex = 40
Cells(row, 4).Interior.ColorIndex = 40
End If
End If
End If
row = row + 1
Loop Until .Cells(row, 1) = ""
End With
End Sub
You should get rid of Select in your code, and make better use of With blocks.
Assuming the first sheet in your workbook is "Sheet1", the following code is a refactored version of your code, getting rid of the Select statements.
Sub Do_ping()
With Worksheets("Sheet1")
row = 2
Do
If .Cells(row, 1) <> "" Then
If IsConnectible(.Cells(row, 1), 2, 100) = True Then
If .Cells(row, 3).Value = nul Then ' has the variable "nul" been defined?
.Cells(row, 1).Interior.Color = RGB(0, 255, 0)
.Cells(row, 1).Font.FontStyle = "bold"
.Cells(row, 1).Font.Size = 14
.Cells(row, 2).Interior.Color = RGB(0, 255, 0)
.Cells(row, 2).Value = Time
Else
.Cells(row, 1).copy Sheets("sheet2").Range("A" & Sheets("sheet2").Rows.Count).End(xlUp).Offset(1, 0)
.Cells(row, 2).copy Sheets("sheet2").Range("B" & Sheets("sheet2").Rows.Count).End(xlUp).Offset(1, 0)
.Cells(row, 5).copy Sheets("sheet2").Range("c" & Sheets("sheet2").Rows.Count).End(xlUp).Offset(1, 0)
.Cells(row, 1).Interior.Color = RGB(0, 255, 0)
.Cells(row, 1).Font.FontStyle = "bold"
.Cells(row, 1).Font.Size = 14
.Cells(row, 2).Interior.Color = RGB(0, 255, 0)
.Cells(row, 2).Value = Time
.Cells(row, 5).ClearContents
End If
'Call siren
Else
'Cells(Row, 2).Formula = "=NOW()-" & CDbl(Now())
'Cells(Row, 1).Interior.Color = RGB(255, 0, 0)
.Cells(row, 3).Value = DateDiff("d", .Cells(row, 2), Now())
'Time Difference. First set the format in cell.
.Cells(row, 4).NumberFormat = "hh:mm:ss"
'/calculate and update
.Cells(row, 4).Value2 = Now() - .Cells(row, 2)
.Cells(row, 5).Value = Hour(.Cells(row, 4).Value2) * 3600 + Minute(.Cells(row, 4).Value2) * 60 + Second(.Cells(row, 4).Value2)
If .Cells(row, 5).Value > 120 Then
.Cells(row, 1).Interior.ColorIndex = 3
.Cells(row, 2).Interior.ColorIndex = 3
.Cells(row, 3).Interior.ColorIndex = 3
.Cells(row, 4).Interior.ColorIndex = 3
Else
.Cells(row, 1).Interior.ColorIndex = 40
.Cells(row, 2).Interior.ColorIndex = 40
.Cells(row, 3).Interior.ColorIndex = 40
.Cells(row, 4).Interior.ColorIndex = 40
End If
End If
End If
row = row + 1
Loop Until .Cells(row, 1) = ""
End With
End Sub
Note: I would strongly recommend that you include Option Explicit as the first line of all your code modules - I suspect that your variable nul should be Null, and the use of Option Explicit would highlight that type of error.
I changed the code and its working
Sub Do_ping()
With Worksheets("Sheet1")
row = 2
Do
If .Cells(row, 1) <> "" Then
If IsConnectible(.Cells(row, 1), 2, 100) = True Then
'Worksheets("sheet1").Select
If Cells(row, 3).Value = nul Then
Sheets("sheet1").Cells(row, 1).Interior.Color = RGB(0, 255, 0)
Sheets("sheet1").Cells(row, 1).Font.FontStyle = "bold"
Sheets("sheet1").Cells(row, 1).Font.Size = 14
Sheets("sheet1").Cells(row, 2).Interior.Color = RGB(0, 255, 0)
Sheets("sheet1").Cells(row, 2).Value = Time
Else
'Worksheets("sheet1").Select
Sheets("sheet1").Cells(row, 1).copy Sheets("sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
Sheets("sheet1").Cells(row, 2).copy Sheets("sheet2").Range("B" & Rows.Count).End(xlUp).Offset(1, 0)
Sheets("sheet1").Cells(row, 5).copy Sheets("sheet2").Range("c" & Rows.Count).End(xlUp).Offset(1, 0)
Sheets("sheet1").Cells(row, 1).Interior.Color = RGB(0, 255, 0)
Sheets("sheet1").Cells(row, 1).Font.FontStyle = "bold"
Sheets("sheet1").Cells(row, 1).Font.Size = 14
Sheets("sheet1").Cells(row, 2).Interior.Color = RGB(0, 255, 0)
Sheets("sheet1").Cells(row, 2).Value = Time
Sheets("sheet1").Cells(row, 5).ClearContents
End If
'Call siren
Else:
'Cells(Row, 2).Formula = "=NOW()-" & CDbl(Now())
'Cells(Row, 1).Interior.Color = RGB(255, 0, 0)
'Worksheets("sheet1").Select
Sheets("sheet1").Cells(row, 3).Value = DateDiff("d", Cells(row, 2), Now())
'Time Difference. First set the format in cell.
Sheets("sheet1").Cells(row, 4).NumberFormat = "hh:mm:ss"
'/calculate and update
Sheets("sheet1").Cells(row, 4).Value2 = Now() - Cells(row, 2)
Sheets("sheet1").Cells(row, 5).Value = Hour(Cells(row, 4).Value2) * 3600 + Minute(Cells(row, 4).Value2) * 60 + Second(Cells(row, 4).Value2)
If Cells(row, 5).Value > 120 Then
'Worksheets("sheet1").Select
Sheets("sheet1").Cells(row, 1).Interior.ColorIndex = 3
Sheets("sheet1").Cells(row, 2).Interior.ColorIndex = 3
Sheets("sheet1").Cells(row, 3).Interior.ColorIndex = 3
Sheets("sheet1").Cells(row, 4).Interior.ColorIndex = 3
Else
'Worksheets("sheet1").Select
Sheets("sheet1").Cells(row, 1).Interior.ColorIndex = 40
Sheets("sheet1").Cells(row, 2).Interior.ColorIndex = 40
Sheets("sheet1").Cells(row, 3).Interior.ColorIndex = 40
Sheets("sheet1").Cells(row, 4).Interior.ColorIndex = 40
End If
End If
End If
row = row + 1
Loop Until .Cells(row, 1) = ""
End With
End Sub
Function IsConnectible(sHost, iPings, iTO)
' Returns True or False based on the output from ping.exe
' sHost is a hostname or IP
' iPings is number of ping attempts
' iTO is timeout in milliseconds
' if values are set to "", then defaults below used
Dim nRes
If iPings = "" Then iPings = 1 ' default number of pings
If iTO = "" Then iTO = 550 ' default timeout per ping
With CreateObject("WScript.Shell")
nRes = .Run("%comspec% /c ping.exe -n " & iPings & " -w " & iTO _
& " " & sHost & " | find ""TTL="" > nul 2>&1", 0, True)
End With
IsConnectible = (nRes = 0)
End Function
Related
I have 2 sheets and 1 macros that pastes values from one to another. The macros is working. I copied it and changed it a bit. But it can't run -
'run time error 9'
which is visibility issue.
All sheets are in same excel file.
original macros code, it works:
Sub original()
For j = 18 To 28
Worksheets("Express_vnzp").Select
srok = Cells(26, j).Value
stav = Cells(31, j).Value
komis = Cells(28, j).Value
stavka_privlech = Cells(29, j).Value
For i = 10 To 12
PD = Cells(i, 17).Value
Worksheets("Ðàñ÷åòû").Select
Cells(3, 2).Value = stav
Cells(4, 2).Value = srok
Cells(5, 2).Value = komis
Cells(7, 2).Value = stavka_privlech
Cells(15, 2).Value = PD
marzha2 = Cells(23, 2).Value
Worksheets("Express_vnzp").Select
Cells(i, j).Value = marzha2
Next
Next
End Sub
I copied and changed i,j - not working.
Sub erj()
For j = 3 To 4
Worksheets("creditcard").Select
srok = Cells(26, j).Value
stav = Cells(31, j).Value
komis = Cells(28, j).Value
stavka_privlech = Cells(29, j).Value
For i = 5 To 6
PD = Cells(i, 17).Value
Worksheets("ras").Select
Cells(3, 2).Value = stav
Cells(4, 2).Value = srok
Cells(5, 2).Value = komis
Cells(7, 2).Value = stavka_privlech
Cells(15, 2).Value = PD
marzha2 = Cells(23, 2).Value
Worksheets("creditcard").Select
Cells(i, j).Value = marzha2
Next
Next
End Sub
gives 'runtime error', its visibility issue.
i have a code written in VBA that will check the date and base on the it will fill the background with the appropriate color.
i have cells (A to G ).
i want to check if the column C is empty i want to keep the row transparent
If IsEmpty(Cells(i, 3)) Then
Range("A" & i & ":G" & i).Interior.Color = xlNone
the problem is that the code step into the if statment to check if empty ... then it step into the last if statement and fill all the empty rows with the specified color.
CODE:
Private Sub CommandButton1_Click()
Dim i As Long
For i = Range("C5000").End(xlUp).Row To 2 Step -1 'Range upto 5000, chnge this as per your requirment'
If IsEmpty(Cells(i, 3)) Then
Range("A" & i & ":G" & i).Interior.Color = xlNone
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) < 0 Then
Cells(i, 3).Interior.Color = vbGreen
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) = 0 Then
Cells(i, 3).Interior.Color = vbYellow
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) >= 1 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) <= 4 Then
Cells(i, 3).Interior.Color = vbRed
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) >= 5 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) <= 10 Then
Cells(i, 3).Interior.Color = vbCyan
Else
Cells(i, 3).Interior.ColorIndex = xlNone
End If
' your 2nd criteria to color the entire row if "G" is not empty
If Trim(Range("G" & i).Value) <> "" Then
Range("A" & i & ":G" & i).Interior.ColorIndex = 15
ElseIf Trim(Range("G" & i).Value) = "" Then
Range("A" & i & ":B" & i).Interior.Color = RGB(255, 189, 189)
Range("D" & i & ":G" & i).Interior.Color = RGB(255, 189, 189)
End If
Next
End Sub
Try this, just an added an extra If clause to your second statement
Private Sub CommandButton1_Click()
Dim i As Long
For i = Range("C5000").End(xlUp).Row To 2 Step -1 'Range upto 5000, chnge this as per your requirment'
If IsEmpty(Cells(i, 3)) Then
Range("A" & i & ":G" & i).Interior.Color = xlNone
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) < 0 Then
Cells(i, 3).Interior.Color = vbGreen
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) = 0 Then
Cells(i, 3).Interior.Color = vbYellow
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) >= 1 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) <= 4 Then
Cells(i, 3).Interior.Color = vbRed
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) >= 5 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) <= 10 Then
Cells(i, 3).Interior.Color = vbCyan
Else
Cells(i, 3).Interior.ColorIndex = xlNone
End If
' your 2nd criteria to color the entire row if "G" is not empty
If Not IsEmpty(Cells(i, 3)) Then
If Trim(Range("G" & i).Value) <> "" Then
Range("A" & i & ":G" & i).Interior.ColorIndex = 15
ElseIf Trim(Range("G" & i).Value) = "" Then
Range("A" & i & ":B" & i).Interior.Color = RGB(255, 189, 189)
Range("D" & i & ":G" & i).Interior.Color = RGB(255, 189, 189)
End If
End If
Next i
End Sub
i have VBA code that check entered dates with the current date and fill the cell in the appropriate color and check if the colomn "F" is not empty it will color the D,E,F columns.
the problem is that i have until now 21 records but the system just color 19 record even so the 2 rows are not empty in the F column.
code:
Private Sub CommandButton1_Click()
Dim i As Long
For i = Range("C5000").End(xlUp).Row To 2 Step -1 'Range upto 5000, chnge this as per your requirment'
If IsEmpty(Cells(i, 3)) Then
Cells(i, 3).Interior.Color = xlNone
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) < 0 Then
Cells(i, 3).Interior.Color = vbGreen
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) = 0 Then
Cells(i, 3).Interior.Color = vbYellow
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) >= 1 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) <= 4 Then
Cells(i, 3).Interior.Color = vbRed
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) >= 5 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) <= 10 Then
Cells(i, 3).Interior.Color = vbCyan
Else
Cells(i, 3).Interior.ColorIndex = xlNone
End If
' your 2nd criteria to color the entire row if "F" is not empty
If Trim(Range("F" & i).Value) <> "" Then Range("D" & i & ":F" & i).Interior.ColorIndex = 15
Next
End Sub
The ElseIf statements will throw Runtime Error 13 if the cells have a non-date value in them. This is caused by trying to convert a non-date value into a date VBA.CDate(Cells(i, 3))
Private Sub CommandButton1_Click()
Dim i As Long
With Worksheets("Sheet1")
For i = Range("C" & .Rows.Count).End(xlUp).Row To 2 Step -1 'Range upto 5000, chnge this as per your requirment'
If IsDate(Cells(i, 3)) Then
Select Case VBA.CDate(.Cells(i, 3)) - VBA.Date()
Case Is < 0
.Cells(i, 3).Interior.Color = vbGreen
Case Is = 0
.Cells(i, 3).Interior.Color = vbYellow
Case Is <= 4
.Cells(i, 3).Interior.Color = vbRed
Case Is <= 10
.Cells(i, 3).Interior.Color = vbCyan
Case Else
.Cells(i, 3).Interior.ColorIndex = xlNone
End Select
Else
.Cells(i, 3).Interior.ColorIndex = xlNone
End If
' your 2nd criteria to color the entire row if "F" is not empty
If Trim(.Range("F" & i).Value) <> "" Then .Range("D" & i & ":F" & i).Interior.ColorIndex = 15
Next
End With
End Sub
Might be something with your data, it runs properly to me. What kind of data do you have in the F column?
Here is my problem:
I have an excel spreadsheet with sheet2 the overall data and sheet3
to sheet8 with data based on years copied from sheet2.
In each sheet(Sheet3-8)values in each row (excluding row 1) are added
and to sum of each row in column D.
To have an interior colour and bold font in column D from row 2 up to
the last row with data I used the following codes (example in
sheet3).
The spreadsheet is to be updated using a command button in sheet2.
When I run the code separately in the VB developer sometimes it works
sometimes it causes run time error 1004.
When I try to update the spreadsheet using the button it always
causes the error.
{Sheet3.Range("D2", Cells(Rows.Count, "D").End(xlUp).Offset(-2, 0)).Interior.Color = RGB(255, 192, 0)}
{Sheet3.Range("D2", Cells(Rows.Count, "D").End(xlUp).Offset(-2, 0)).Font.Bold=True}
The full code is as shown below:
{Sub YearlyForcast2011_2012()
Sheet3.Columns("D").HorizontalAlignment = xlRight
Dim j As Integer
Dim lastrow2 As Long
Dim sumrange As Long
lastrow2 = Sheet3.Cells(Rows.Count, 1).End(xlUp).Row
For j = 2 To lastrow2
Sheet3.Cells(j, 4).Value = Sheet3.Cells(j, 5).Value + Sheet3.Cells(j, 6).Value + Sheet3.Cells(j, 7).Value + Sheet3.Cells(j, 8) + Sheet3.Cells(j, 9).Value + Sheet3.Cells(j, 10).Value + Sheet3.Cells(j, 11).Value + Sheet3.Cells(j, 12).Value + Sheet3.Cells(j, 13).Value + Sheet3.Cells(j, 14).Value + Sheet3.Cells(j, 15).Value + Sheet3.Cells(j, 16).Value
Next j
sumrange = Sheet3.Cells(Rows.Count, "D").End(xlUp).Row
Sheet3.Range("D" & sumrange + 2).Formula = "=SUM(D2:D" & sumrange & ")"
Sheet3.Range("D" & sumrange + 2).Font.Bold = True
Sheet3.Range("D" & sumrange + 2).Font.Size = 12
Sheet3.Range("D" & sumrange + 2).Font.Color = RGB(255, 0, 0)
Sheet3.Range("D2", Cells(Rows.Count, "D").End(xlUp).Offset(-2, 0)).Interior.Color = RGB(255, 192, 0)
Sheet3.Range("D2", Cells(Rows.Count, "D").End(xlUp).Offset(-2, 0)).Font.Bold=True
Sheet3.Range("c" & sumrange + 2).Value = "TOTAL 2011-2011 YEARLY FORCAST"
Sheet3.Range("c" & sumrange + 2).Font.Bold = True
Sheet3.Range("c" & sumrange + 2).Font.Size = 12
Sheet3.Range("c" & sumrange + 2).Font.Color = RGB(255, 0, 0)
Sheet3.Range("c" & sumrange + 2).HorizontalAlignment = xlRight
Application.ScreenUpdating = False
Application.CutCopyMode = False
End Sub
}
Can someone help me to avoid the error and update the spreadsheet keeping the interior color and bold font in each D column of the sheets?
Try this:
Before lines
Sheet3.Range("D2", Cells(Rows.Count, "D").End(xlUp).Offset(-2, 0)).Interior.Color = RGB(255, 192, 0)
Sheet3.Range("D2", Cells(Rows.Count, "D").End(xlUp).Offset(-2, 0)).Font.Bold=True
add
lastrow2 = Sheet3.Cells(Rows.Count, "D").End(xlUp).Row
and change your lines to this:
Sheet3.Range("D2", Cells(lastrow2, "D")).Interior.Color = RGB(255, 192, 0)
Sheet3.Range("D2", Cells(lastrow2, "D")).Font.Bold = True
You could also change this:
For j = 2 To lastrow2
Sheet3.Cells(j, 4).Value = Sheet3.Cells(j, 5).Value + Sheet3.Cells(j, 6).Value + Sheet3.Cells(j, 7).Value + Sheet3.Cells(j, 8) + Sheet3.Cells(j, 9).Value + Sheet3.Cells(j, 10).Value + Sheet3.Cells(j, 11).Value + Sheet3.Cells(j, 12).Value + Sheet3.Cells(j, 13).Value + Sheet3.Cells(j, 14).Value + Sheet3.Cells(j, 15).Value + Sheet3.Cells(j, 16).Value
Next j
To this:
Sheet3.Range("D2:D" & lastrow2).Formula = "=SUM(E2:T2)"
To run through sheets 3-8 the whole code will look like that (remember the j is the index of the sheet! Adjust if necessary):
Sub YearlyForcast2011_2012()
Dim j As Integer
Dim lastrow2 As Long
Dim sumrange As Long
For j = 3 To 8
Sheets(j).Columns("D").HorizontalAlignment = xlRight
lastrow2 = Sheets(j).Cells(Rows.Count, 1).End(xlUp).Row
Sheets(j).Range("D2:D" & lastrow2).Formula = "=SUM(E2:T2)"
sumrange = Sheets(j).Cells(Rows.Count, "D").End(xlUp).Row
Sheets(j).Range("D" & sumrange + 2).Formula = "=SUM(D2:D" & sumrange & ")"
Sheets(j).Range("D" & sumrange + 2).Font.Bold = True
Sheets(j).Range("D" & sumrange + 2).Font.Size = 12
Sheets(j).Range("D" & sumrange + 2).Font.Color = RGB(255, 0, 0)
lastrow2 = Sheets(j).Cells(Rows.Count, "D").End(xlUp).Row
Sheets(j).Range("D2", Cells(lastrow2, "D")).Interior.Color = RGB(255, 192, 0)
Sheets(j).Range("D2", Cells(lastrow2, "D")).Font.Bold = True
Sheets(j).Range("c" & sumrange + 2).Value = "TOTAL 2011-2011 YEARLY FORCAST"
Sheets(j).Range("c" & sumrange + 2).Font.Bold = True
Sheets(j).Range("c" & sumrange + 2).Font.Size = 12
Sheets(j).Range("c" & sumrange + 2).Font.Color = RGB(255, 0, 0)
Sheets(j).Range("c" & sumrange + 2).HorizontalAlignment = xlRight
Next j
Application.ScreenUpdating = False
Application.CutCopyMode = False
End Sub
I am running the following macro to apply conditional formatting on a range of cells. What I want is the macro to be triggered anytime those cell values change. The cell values are not changed manually (i.e. they aren't selected by a user and changed), they change automatically because they contain formulas linked to cells in other spreadsheets.
What is the most efficient of doing this?
Sub TestSub3()
Dim i As Integer, j As Integer
For i = 5 To 27
If i Mod 2 <> 0 Then
For j = 2 To 16
If Cells(i, j) = 0 Then
Cells(i, j).Interior.Color = RGB(146, 208, 80) 'light green fill
Cells(i, j).Font.Color = RGB(0, 176, 80) 'dark green font
ElseIf Cells(2, 1) - Cells(i, 1) > 60 And Cells(i, j) > 0 Then
Cells(i, j).Interior.Color = RGB(255, 0, 0) 'red fill
Cells(i, j).Font.Color = RGB(255, 255, 0) 'yellow font
ElseIf Cells(2, 1) - Cells(i, 1) > 52 And Cells(i, j) > 0 Then
Cells(i, j).Interior.Color = RGB(255, 192, 0) 'orange fill, black font
ElseIf Cells(2, 1) - Cells(i, 1) > 45 And Cells(i, j) > 0 Then
Cells(i, j).Interior.Color = RGB(255, 255, 0) 'yellow fill, black font
End If
Next j
End If
Next i
End Sub
You can use "Private Sub Workbook_SheetCalculate(ByVal Sh As Object)" subroutine to perform change function. Paste the below code in the Microsoft excel object-->sheetname place. Look into the attached picture too
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
Dim i As Integer, j As Integer
For i = 5 To 27
If i Mod 2 <> 0 Then
For j = 2 To 16
'Debug.Print Cells(i, j)
If Cells(i, j).Value = 0 Then
Cells(i, j).Interior.Color = RGB(146, 208, 80) 'light green fill
Cells(i, j).Font.Color = RGB(0, 176, 80) 'dark green font
ElseIf (Cells(2, 1).Value - Cells(i, 1).Value) > 60 And Cells(i, j).Value > 0 Then
Cells(i, j).Interior.Color = RGB(255, 0, 0) 'red fill
Cells(i, j).Font.Color = RGB(255, 255, 0) 'yellow font
ElseIf (Cells(2, 1).Value - Cells(i, 1).Value) > 52 And Cells(i, j).Value > 0 Then
Cells(i, j).Interior.Color = RGB(255, 192, 0) 'orange fill, black font
ElseIf (Cells(2, 1).Value - Cells(i, 1).Value) > 45 And Cells(i, j).Value > 0 Then
Cells(i, j).Interior.Color = RGB(255, 255, 0) 'yellow fill, black font
End If
Next j
End If
End Sub
You can also look into link for your reference
https://msdn.microsoft.com/en-us/library/office/ff839775.aspx