|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Private Sub Worksheet_Change(ByVal Target As Range)
' 判断是否是B2单元格发生变化
If Not Intersect(Target, Worksheets("Sheet2").Range("B2")) Is Nothing Then
' 调用修改颜色的子程序
Call ChangeColorIfNotABC
End If
End Sub
Sub ChangeColorIfNotABC()
' 声明变量
Dim ws As Worksheet
Dim cellValue As String
Dim shape As Shape
Dim textColor As Long
Dim fillColor As Long
' 设置工作表对象为Sheet2
Set ws = ThisWorkbook.Sheets("Sheet2")
' 获取B2单元格的值
cellValue = UCase(Trim(ws.Range("B2").Value))
' 如果B2单元格的值不是A、B或C且不是空值
If cellValue <> "" And cellValue <> "A" And cellValue <> "B" And cellValue <> "C" Then
' 遍历所有图形对象并修改其颜色
For Each shape In ws.Shapes
On Error Resume Next
' 随机生成颜色
textColor = RGB(Int(Rnd * 256), Int(Rnd * 256), Int(Rnd * 256))
fillColor = RGB(Int(Rnd * 256), Int(Rnd * 256), Int(Rnd * 256))
' 修改文字颜色
If shape.TextFrame2.HasText Then
shape.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = textColor
End If
' 修改填充颜色
If Not shape.Fill Is Nothing Then
shape.Fill.ForeColor.RGB = fillColor
End If
On Error GoTo 0
Next shape
End If
End Sub
|
|