请帮忙看看这段excel vba代码怎么会是这个结果呢!!

来源:百度知道 编辑:UC知道 时间:2024/05/26 06:43:05
代码如下:我是想把大于900的数字筛选出来,但是为什么有的变了颜色,而有的单元格大于900还是没有变颜色呢??真是奇怪,难道我的程序又问题?
Sub repl()
Worksheets("sheet1").Rows.ClearContents
Worksheets("sheet1").Activate
Dim rangeA As Range
Set rangeA = Worksheets("Sheet1").Range(Cells(1, 1), Cells(100, 10))
rangeA.Formula = "=rand()*1000"
rangeA.Font.Color = RGB(255, 0, 0)
For rwIndex = 1 To 100
For colIndex = 1 To 10
With Worksheets("Sheet1").Cells(rwIndex, colIndex)
If .Value > 900 Then
temp = Int(.Value)
.Value = "rel_" & temp
.Font.Color = RGB(45, 145, 154)
End If
End With
Next colIndex
Next rwIndex
End Sub
这张图片才对哈! 在我选中的单元格就是我说的那种情况!

程序功能是正常的,产生有的数据没能达到你的要求,是因为你采用了随机函数,产生了数据后,是会重新刷新的,而刷新的数据你又没重新进行判断,从而导致了你现在的结果.

还有一个错误,你清除单元格格式的命令也不对

程序可修改如下:

Sub repl()
Dim dat
Cells.Select
Selection.ClearFormats
For rwIndex = 1 To 100
For colIndex = 1 To 10
With Worksheets("Sheet1").Cells(rwIndex, colIndex)
Randomize
dat = Int(Rnd() * 1000 + 1)
.Value = dat
If dat > 900 Then
.Value = "rel_" & Int(.Value)
.Font.Color = RGB(45, 145, 154)
else
.font.color=rgb(255,0,0)
End If
End With
Next colIndex
Next rwIndex
End Sub

你试过我这个小程序没有?应该能达到你的要求的,

我这个小程序,可以选出大于900的,然后用不同颜色表示出来,不会漏选的