excel中怎样用VBA实现单元格的字体显色?
条件3,我还没做完,以下代码可满足条件1和2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub px()
Dim a1, a2, a3, a4, a5, a6, a7, a8, b1, b2, b3, b4, b5, b6, b7, b8 As Integer
a3 = 0
A9字符长度 = Len(Sheet1.Cells(9, 1))
A10字符长度 = Len(Sheet1.Cells(10, 1))
'a1定义为列的循环,按B列包含多少行有效数而定循环数
'a2定义为行循环 , 按A9和A10的个数而定循环次数
For a1 = 1 To 10
For a2 = 1 To A9字符长度
If Len(Sheet1.Cells(a1, 2)) - Len(Replace(Sheet1.Cells(a1, 2), Mid(Sheet1.Cells(9, 1), a2, 1), "")) > 0 Then
'a3定义为一个累加变量,存放满足A9条件的次数
a3 = a3 + 1
End If
Next a2
'Sheet1.Cells(a1, 3) = a3
If a3 = 3 Then
Sheet1.Cells(a1, 2).Font.Color = vbRed
End If
a3 = 0
Next a1
'a1定义为列的循环,按B列包含多少行有效数而定循环数
'a2定义为行循环 , 按A9和A10的个数而定循环次数
For a1 = 1 To 10
For a2 = 1 To A10字符长度
If Len(Sheet1.Cells(a1, 2)) - Len(Replace(Sheet1.Cells(a1, 2), Mid(Sheet1.Cells(10, 1), a2, 1), "")) > 0 Then
'a3定义为一个累加变量,存放满足A9条件的次数
a3 = a3 + 1
End If
Next a2
'Sheet1.Cells(a1, 4) = a3
If a3 = 3 Then
Sheet1.Cells(a1, 2).Font.Color = vbRed
End If
a3 = 0
Next a1
For a1 = 1 To 10
If Not Sheet1.Cells(a1, 2).Find("05") Is Nothing Then
'Sheet1.Cells(a1, 5) = 1
Sheet1.Cells(a1, 2).Font.Color = vbRed
End If
If Not Sheet1.Cells(a1, 2).Find("50") Is Nothing Then
'Sheet1.Cells(a1, 5) = 1
Sheet1.Cells(a1, 2).Font.Color = vbRed
End If
If Not Sheet1.Cells(a1, 2).Find("24") Is Nothing Then
'Sheet1.Cells(a1, 5) = 1
Sheet1.Cells(a1, 2).Font.Color = vbRed
End If
If Not Sheet1.Cells(a1, 2).Find("42") Is Nothing Then
'Sheet1.Cells(a1, 5) = 1
Sheet1.Cells(a1, 2).Font.Color = vbRed
End If
If Not Sheet1.Cells(a1, 2).Find("69") Is Nothing Then
'Sheet1.Cells(a1, 5) = 1
Sheet1.Cells(a1, 2).Font.Color = vbRed
End If
If Not Sheet1.Cells(a1, 2).Find("96") Is Nothing Then
'Sheet1.Cells(a1, 5) = 1
Sheet1.Cells(a1, 2).Font.Color = vbRed
End If
Next a1
End Sub
不行,你在A10中换一个数121看,
恩,我的代码比较落后,楼上大神代码犀利,膜拜中。。。
代码如下,详见附件
Private Sub CommandButton1_Click()
Dim Arr, Ar1(1 To 3)
Range("B:B").Font.ColorIndex = 0
Arr = Range([B1], [B65536].End(3))
For i = 1 To UBound(Arr)
s = Mid(Arr(i, 1), InStr(Arr(i, 1), ".") + 1)
If InStr(s, "05") > 0 Or InStr(s, "50") > 0 Or InStr(s, "24") > 0 Or InStr(s, "42") > 0 Or InStr(s, "69") > 0 Or InStr(s, "96") > 0 Then
Range("B" & i).Font.ColorIndex = 3
Else
For j = 9 To 10
d = Range("A" & j)
If InStr(s, Left(d, 1)) > 0 And InStr(s, Mid(d, 2, 1)) > 0 And InStr(s, Right(d, 1)) > 0 Then Range("B" & i).Font.ColorIndex = 3
Next
End If
Next
End Sub
With Selection
.WrapText = True '自动换行
.Interior.Color = 65535 '背景 黄
.Font.Name = "黑体" '字体
.Font.Size = 10 '字号
.Font.Color = -16776961 '颜色 红
End With
你直接点击录制宏然后改变下颜色你看下代码就能知道代码怎么写了