请帮我看看这段代码有什么问题谢谢了
在excel里我想把所有0.5的值标成绿色,所有2的值标成红色,结果出来不能全部标注,怎么也弄也标不全,麻烦帮我看看代码问题在哪里,谢谢了!宏1 宏
'
Dim i, j, x, y, z As Integer
Dim a, b, c As Characters
Sheets("sheet2").Select
'
For i = 2 To 91
For j = 4 To 34
If Cells(i, j).Value = 2 Then
Cells(i, j).Interior.Color = (255)
Else
If Cells(i, j).Value = 0.5 Then
Cells(i, j).Interior.Color = RGB(255, 255, 0)
End If
End If
Next j
Next i
这个需求用条件格式就行 确实用条件格式就行了
如果一定要用代码,首先,Cells(i, j).Interior.Color = RGB(255, 255, 0)这句的效果是黄色……
其次,Sheets("sheet2").Select你操作的是sheet2.
再次
For i = 2 To 91
For j = 4 To 34
你可以把91,34改成 sheet1.usedrange.rows.count 和 sheet1.UsedRange.Columns.Count
先试试这些吧 For i = 2 To 91
For j = 4 To 34
If val(Cells(i, j)) = 2 Then
Cells(i, j).Interior.Color = (255)
Else
If val(Cells(i, j)) = 0.5 Then
Cells(i, j).Interior.Color = RGB(255, 255, 0)
End If
End If
Next j
Next i
标不全是因为你这边循环i,j的范围写死了,for i = 2 to sheet1.usedRange.Rows.count ,for j = 4 to sheet1.usedRange.Columns.count ,这样会把sheet1中有效的工作区域的内容全部覆盖到。这样试试,应该没问题。 Public Sub test()
Dim sheet As Worksheet
Set sheet = Sheet1
Dim rowNum As Integer
Dim colNum As Integer
Dim i As Integer
Dim j As Integer
rowNum = sheet.UsedRange.EntireRow.Count
colNum = sheet.UsedRange.EntireColumn.Count
For i = 1 To rowNum
For j = 1 To colNum
If sheet.Cells(i, j).Value = 0.5 Then
sheet.Cells(i, j).Font.Color = -11489280
End If
If sheet.Cells(i, j).Value = 2 Then
sheet.Cells(i, j).Font.Color = -16776961
End If
Next
Next
End Sub
这段代码是把0.5值的颜色变成绿色,值为2的颜色变成红色。
你上面那个写法是把单元格的颜色刷成黄色,copy下自己看下。
补充:VB , VBA