VBA: Color cells in table based on multiple condition
Solved/Closed
abdelfatah_0230
Posts
73
Registration date
Thursday 18 July 2019
Status
Member
Last seen
23 July 2022
-
22 Mar 2020 à 11:24
abdelfatah_0230 Posts 73 Registration date Thursday 18 July 2019 Status Member Last seen 23 July 2022 - 24 Mar 2020 à 13:18
abdelfatah_0230 Posts 73 Registration date Thursday 18 July 2019 Status Member Last seen 23 July 2022 - 24 Mar 2020 à 13:18
Related:
- Based on the values in cells b77
- Vba find - Guide
- Vba case like - Guide
- Vba vlookup - Excel Forum
- How to open vba in excel - Guide
- Vba timer - Guide
1 response
TrowaD
Posts
2921
Registration date
Sunday 12 September 2010
Status
Contributor
Last seen
27 December 2022
555
24 Mar 2020 à 12:39
24 Mar 2020 à 12:39
Hi Abdel,
That is because of how you structured your IF statements.
5th code line:
If Cells(i, 5) < Cells(i, 4) Then
When this condition is not met, then the code skips to code line 12, thus skipping the other IF statements.
Here is your code, where each IF statement is handled one after the other:
The best way to check what is going on with your code is by placing your cursor somewhere in your code and then press F8 to go by each code line. By pressing F8 multiple times you can check where in your code it deviates from what you had in mind.
Best regards,
Trowa
That is because of how you structured your IF statements.
5th code line:
If Cells(i, 5) < Cells(i, 4) Then
When this condition is not met, then the code skips to code line 12, thus skipping the other IF statements.
Here is your code, where each IF statement is handled one after the other:
Sub dat() Dim lr As Integer, i As Integer lr = Cells(Rows.Count, "d").End(xlUp).Row For i = 2 To lr If Cells(i, 5) < Cells(i, 4) Then Cells(i, 5).Interior.Color = vbRed If Cells(i, 6) = Cells(i, 4) + 69 Then Cells(i, 6).Interior.Color = vbYellow If Month(CDate(Cells(i, 6))) = Month(Date) Then Cells(i, 6).Interior.Color = vbRed Next i End Sub
The best way to check what is going on with your code is by placing your cursor somewhere in your code and then press F8 to go by each code line. By pressing F8 multiple times you can check where in your code it deviates from what you had in mind.
Best regards,
Trowa
24 Mar 2020 à 13:18