问题
i have VBA code that compare dates of the user input with the current dates and fill the background by the appropriate color. all it works fine.
now i need to make the system to check if the cell in column F of the selected row is not empty i need to colored the column D,E,F in gray color.
code:
Private Sub CommandButton1_Click()
Dim i As Integer
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
Next
End Sub
i will appreciate any help
回答1:
You need to check also if Trim(Range("F" & i).Value) <> ""
in the relevant criterias.
I've also modifed the logic of your current code to be shorter and faster to run through (switched to Select Case
, etc.)
Code
Private Sub CommandButton1_Click()
Dim i As Long
Dim NumofDays As Long
For i = Range("C5000").End(xlUp).Row To 2 Step -1 'Range upto 5000, chnge this as per your requirment'
Cells(i, 3).Interior.Color = xlNone ' set as inital color, only change if all criterias are met
NumofDays = CDate(Cells(i, 3)) - Date
Select Case NumofDays
Case Is < 0
Cells(i, 3).Interior.Color = vbGreen
Case 0
Cells(i, 3).Interior.Color = vbYellow
Case 1 To 4
Cells(i, 3).Interior.Color = vbRed
Case 5 To 10
Cells(i, 3).Interior.Color = vbCyan
End Select
' 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.Color = ... ' selectyourcolor
Next i
End Sub
回答2:
Conditional Formatting is easiest, just a few clicks.
- Start with selecting the cells the background color of which you want to change.
- Create a new formatting rule by clicking Conditional Formatting > New Rule... on the Home tab.
- On the Home tab, click Conditional Formatting > New Rule... In the "New Formatting Rule" dialog window that opens, choose the option "Use a formula to determine which cells to format" and enter the following formula in the "Format values where this formula is true" field: =$C2>4
- Open the 'New Formatting Rule' dialog and enter the needed formula. Instead of C2, you enter a cell that contains the value you want to check in your table and put the number you need instead of 4. And naturally, you can use the less (<) or equality (=) sign so that your formulas will read =$C2<4 and =$C2=4, respectively.
- Also, pay attention to the dollar sign $ before the cell's address, you need to use it to keep the column letter the same when the formula gets copied across the row. Actually, it is what makes the trick and applies formatting to the whole row based on a value in a given cell.
- Click the "Format..." button and switch to Fill tab to choose the background color. If the default colors do not suffice, click the "More Colors..." button to pick the one to your liking, and then click OK twice.
来源:https://stackoverflow.com/questions/46807896/vba-excel-how-to-check-if-cell-not-empty-and-color-the-entire-row