excel vba给有内容的单元填充颜色

1。vba给有内容的单元填充颜色
2。A7:AQ900(空单元颜色不变,有内容的单元填充“黄”色)
3。空单元颜色不变
4。有内容的单元填充“黄”色

第1个回答  2015-04-27
Sub jk()
Dim i, j
Application.ScreenUpdating = False
Range("a7:aq900").Select
For i = 1 To Selection.Count
If Selection(i) <> "" Then
With Selection(i).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Else: With Selection(i).Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
Next
Application.ScreenUpdating = True
End Sub追问

只是给有内容的填充颜色,没有内容的颜色不能变成底色(原来空单元的原色不能变)

追答

Sub jk()
Dim i, j
Application.ScreenUpdating = False
Range("a7:aq900").Select
For i = 1 To Selection.Count
If Selection(i) "" Then
With Selection(i).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With

End If
Next
Application.ScreenUpdating = True
End Sub

本回答被提问者采纳
第2个回答  2016-01-12
如你的数据在A列,代码如下。
sub aa ()
for x =1 to range("A65536").end(xlup).row'建立循环
if cells(x,1)=6 then'判断单元格值
cells(x,1).interior.colorIndex = 3'单元格值等于6时,该单元格填充红色
end if
next x
end sub
第3个回答  2015-04-27
VBA 也可以设置条件格式的

Sub Macro1()
Range("a:a").FormatConditions.Delete
Range("a:a").FormatConditions.Add Type:=xlExpression, Formula1:="=IF(A1<>"""",1,0)"
Range("a:a").FormatConditions(1).Interior.ColorIndex = 8
End Sub追问

VBA有内容的单元填充“黄”色

追答

ColorIndex = 8 自己设置颜色数值

追问

不行的,有内容的单元填充,你的是乱了,没内容的也填充了一部分,有内容的也填充了一部分

追答

Sub Macro1()
Range("A7:AQ900").FormatConditions.Delete
Range("A7").active
Range("A7:AQ900").FormatConditions.Add Type:=xlExpression, Formula1:="=IF(A7"""",1,0)"
Range("A7:AQ900").FormatConditions(1).Interior.ColorIndex = 8
End Sub

追问

Range("A7").active

相似回答