我在excel中有一个包含多列数据的工作表,如果数字是<我想要用红色突出显示名为“FCCR”的一个特定列的单元格。 1.0和另一列的内容(用于解释原因为< 1.0)是空的。
我在想我使用以下逻辑找到FCCR列:
Dim fccrHeader as Range
Set fccrHeader = ActiveSheet.Rows(1).Find("FCCR", lookat:=xlWhole)
我遇到的问题是如何突出显示单元格IF,并且仅在FCCR值小于1.0且注释列为空时才考虑。我是否有正确的想法来查找FCCR列标题?非常感谢提前。
答案 0 :(得分:0)
我已经尝试并测试了以下内容,它会按照您的要求执行:
Sub foo()
Dim val As Variant
Dim ColumnNumber As Long
Set val = Sheet1.Rows(1).Find("FCCR", lookat:=xlWhole)
ColumnNumber = val.Column
LastRow = Sheet1.Cells(Sheet1.Rows.Count, ColumnNumber).End(xlUp).Row
For i = 1 To LastRow
If Sheet1.Cells(i, 12).Value = "" Then 'Change the number 12 to reflect the column which might be empty
If Sheet1.Cells(i, ColumnNumber).Value < 1 Then
Sheet1.Cells(i, ColumnNumber).Interior.ColorIndex = 3 '3 is equals to red
End If
End If
Next i
End Sub
答案 1 :(得分:0)
答案 2 :(得分:0)
尝试这样的事情?我将它放入Active-X控制按钮,但如果你愿意,可以在worksheet_change上执行。与你可能从其他用户那里找到的相比,它有点啰嗦
Private Sub CommandButton1_Click()
Dim fccrHeader As Range
Dim col, otherCol, row As Integer
Set fccrHeader = ActiveSheet.Rows(1).Find("FCCR", lookat:=xlWhole)
col = fccrHeader.Column
otherCol = 7 ' comment column
For row = 2 To 10 'end of data constant or use search
If Cells(row, col).Value < 1 And Cells(row, otherCol).Value = "" Then
'also highlights if both are empty, add that logic to check for ""
With Cells(row, col).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
Next row
End Sub