条件格式行VBA
我一直在寻找类似的话题,但找不到解决问题的合适方法 目标:条件格式行VBA,vba,excel,Vba,Excel,我一直在寻找类似的话题,但找不到解决问题的合适方法 目标: 我有一张excel表格,上面有关于我的工作研究的数据: -公司,职位,申请发送日期,…,最后是申请日期和今天日期之间经过的天数 我想要一个宏来检查每个位置等待的天数(列I中的值),并有条件地将颜色应用于整行。这样我就可以很好地跟进我的工作研究 使用的代码 Sub outofdate() Range("I4").Select Range(Selection, Selection.End(xlDown)).Select numrow = S
我有一张excel表格,上面有关于我的工作研究的数据: -公司,职位,申请发送日期,…,最后是申请日期和今天日期之间经过的天数 我想要一个宏来检查每个位置等待的天数(列I中的值),并有条件地将颜色应用于整行。这样我就可以很好地跟进我的工作研究 使用的代码
Sub outofdate()
Range("I4").Select
Range(Selection, Selection.End(xlDown)).Select
numrow = Selection.Rows.Count
For r = 4 To numrow
c = Cells(r, 9).Value
Select Case c
Case Is = 7 < c < 10
ColorIndex = 4
Case Is = 10 < c < 15
ColorIndex = 45
Case Is = 15 < c < 21
ColorIndex = 3
End Select 'c
With Range(Cells(r, 1), Cells(r, 9)).Select
Selection.Interior.Color = ColorIndex
End With
Next
End Sub
Sub outofdate()
范围(“I4”)。选择
范围(选择,选择。结束(xlDown))。选择
numrow=Selection.Rows.Count
对于r=4到numrow
c=单元格(r,9)。数值
选择案例c
病例Is=7
面临的问题当我运行宏时,A4:I12范围内的单元格都是黑色的,尽管我没有设置一个指向黑色的颜色索引
感谢您的时间,我一直在试图找出哪里出了问题,但我的知识似乎到此为止。我使用了if和else if而不是select case
Sub outofdate()
Dim numrow As Long
numrow = ActiveSheet.Range("I" & Rows.Count).End(xlUp).Row
For r = 4 To numrow
c = Cells(r, 9).Value
If c > 7 And c < 10 Then
Cells(r, 9).EntireRow.Interior.ColorIndex = 4
ElseIf c > 10 And c < 15 Then
Cells(r, 9).EntireRow.Interior.ColorIndex = 45
ElseIf c > 15 And c < 21 Then
Cells(r, 9).EntireRow.Interior.ColorIndex = 3
End If
Next
End Sub
Sub outofdate()
暗如长虹
numrow=ActiveSheet.Range(“I”和Rows.Count).End(xlUp).Row
对于r=4到numrow
c=单元格(r,9)。数值
如果c>7且c<10,则
单元格(r,9).EntireRow.Interior.ColorIndex=4
如果c>10且c<15
单元格(r,9).EntireRow.Interior.ColorIndex=45
如果c>15和c<21
单元格(r,9).EntireRow.Interior.ColorIndex=3
如果结束
下一个
端接头
问题在于,如果黑色的代码为零,并且颜色变量从未从零更改,那么它将为您提供黑色。此外,“选择案例”的符号也不能正常工作
希望这对您有所帮助:)下面的@pnuts注释-选择您的范围,然后在功能区上选择主页,然后选择条件格式。有几个选项可以创建格式,甚至有一个公式选项。我不太清楚为什么要用VBA来创建格式,你不能用Excel中的内置条件格式选项轻松地创建格式吗?谢谢你的回答。是的,我知道我可以使用excel选项使用条件格式,但是出于好奇,我想通过在VBA中使用它来理解它背后的逻辑。关于变量
ColorIndex
,我会使用另一个名称,因为它是VBA中使用的特定单词(单元格(1,1)。Interior.ColorIndex=4
)。我倾向于使用clrIndex
或其他东西将其设置为变量。确实有效,但它只是突出显示单元格而不是整行。@M.O-简单的修复方法,只需将以.ColorIndex
结尾的行更改为单元格(r,9).EntireRow.Interior.ColorIndex
(添加EntireRow
位)。然后它将突出显示整条线@布鲁斯韦恩,谢谢。我已对解决方案进行了更改。@M.O现在检查答案确实帮助很大!谢谢我现在理解了逻辑,我唯一没有得到的一点是“lValue=r.Value”“lValue=r.Value”用于获取循环中单元格的值。如果你是对的,我将值放在变量的某个范围内,以备以后检查。
Option Explicit
Sub outofdate()
Dim rWorkRange As Range
Dim r As Range
Dim lColorIndex As Long
Dim lValue As Long
' Set the range for the loop.
Set rWorkRange = Range(Range("I4"), Range("I4").End(xlDown))
' Loop through the range.
For Each r In rWorkRange
lValue = r.Value
' Select each case, make sure that the else sets the
' lColorIndex variable to the default "0"
Select Case lValue
Case 7 To 10
lColorIndex = 4
Case 10 To 15
lColorIndex = 45
Case 15 To 21
lColorIndex = 3
Case Else
lColorIndex = 0
End Select
' Check for the "0" value (This is the black color)
' if the lColorIndex is zero then skip.
If Not lColorIndex = 0 Then
Cells(r.Row, 1).Resize(, 9).Interior.ColorIndex = lColorIndex
End If
Next r
End Sub