我想比较2个不同工作表中的2列(每张表中的相同列),然后比较它们是增加还是减少,然后将单元格颜色设置为红色或绿色以指示是增加还是减少。但是我的公式不起作用...... 我尝试使用activecell在同一张纸上的列,以及activecell偏移并且它有效,但我似乎无法在另一张纸中引用它? 但是我的(不工作)代码......
Sub test3()
Range("A1").Select
Do
If ActiveWorkbook.worksheets(“Sheet1!”).Cell(ActiveCell) > ActiveWorkbook.worksheets(“Sheet2!”).Cell(“A1”) Then
ActiveCell.Interior.Color = RGB(255, 0, 0)
ElseIf ActiveWorkbook.worksheets(“Sheet1!”).Cell(ActiveCell) < ActiveWorkbook.worksheets(“Sheet2!”).Cell(“A1”) Then
ActiveCell.Interior.Color = RGB(0, 255, 0)
Else
End If
ActiveCell.Offset(1, 0).Select
Loop Until IsEmpty(ActiveCell)
End Sub
答案 0 :(得分:1)
试试这个,只需要将列设置为你需要的任何列索引。
Dim row As Integer
Dim column As Integer
row = 1
column = 1
ActiveWorkbook.Worksheets("Sheet1!").Cells.Interior.Color = RGB(255, 255, 255)
Do While ActiveWorkbook.Worksheets("Sheet1!").Cells(row, column) <> ""
If ActiveWorkbook.Worksheets("Sheet1!").Cells(row, column) <> ActiveWorkbook.Worksheets("Sheet2!").Cells(row, column) Then
If ActiveWorkbook.Worksheets("Sheet1!").Cells(row, column) > ActiveWorkbook.Worksheets("Sheet2!").Cells(row, column) Then
ActiveWorkbook.Worksheets("Sheet1!").Cells(row, column).Interior.Color = RGB(255, 0, 0)
Else
ActiveWorkbook.Worksheets("Sheet1!").Cells(row, column).Interior.Color = RGB(0, 255, 0)
End If
End If
row = row + 1
Loop
答案 1 :(得分:0)
这样做 - 只需更改colorRng
引用以满足您的需求:
Sub ColorCode()
Dim cl As Range, colorRng As Range
Set colorRng = Worksheets(1).Range("A1:A10")
For Each cl In colorRng
If cl > Worksheets(2).Cells(cl.Row, cl.Column) Then
cl.Interior.Color = RGB(255, 0, 0)
Else
cl.Interior.Color = RGB(0, 255, 0)
End If
Next cl
End Sub