0

我想比较来自 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
4

2 回答 2

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
于 2013-10-16T09:49:14.053 回答
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
于 2013-10-16T11:04:51.930 回答