2015-06-28 69 views
0

我有一个Excel工作表,其中几个值相加形成总计。然后我必须手动检查每个总数是否正确。一旦总计值,我想突出显示单个单元格并对它们进行颜色编码。我想通过一个宏如何在excel中对单元格进行颜色编码。有没有办法找出哪些细胞已从公式中加总?Excel VBA - 突出显示已经求和的单元格

例如;
如果总和值= A1 + A4 + A7,则宏应对这些单元格进行颜色编码。

任何帮助,非常感谢。

+1

所有的“和细胞”是一系列细胞+细胞+细胞(或更多)?您可以编写一个宏,将它们拆分为+符号的数组(我们称之为'summedCells'),然后打开每个数组元素的背景颜色。 '范围(summedCells(i))。Interior.ColorIndex = 5'。 – nhee

回答

0

这可能会给你一个想法。以下代码会将所选单元格的所有直接先例浅绿色。例如,在A10我有公式

=A1+A4+A7 

如果我调用下面的宏,而选择A10然后A1,A4和A7为绿色:

Sub ColorSummands() 
    Dim R As Range 
    Set R = Selection 
    R.DirectPrecedents.Interior.Color = 5296274 'light green 
End Sub 

这将是很容易修改该代码以便输入框弹出要求用户选择颜色

0

让我知道下面的VBA代码是否适合您。 它将允许您突出显示任何工作表中的任何单元格(只要它在同一工作簿中)。它可以帮助您进行加,减,乘或除 - 并且它甚至可以一次处理多个公式单元格。

Sub color_cells_in_formula() 

Dim workrng As Range 

Dim lcolor As Long 

    Set workrng = Application.Selection 

    Set workrng = Application.InputBox("Range", xTitleId, workrng.Address, Type:=8) 

If Application.Dialogs(xlDialogEditColor).Show(10, 0, 125, 125) = True Then 

    lcolor = ActiveWorkbook.Colors(10) 

Else 

End If 

For Each cell In workrng 

If cell.Value <> "" Then 

    Dim result As String 

    result = cell.Formula 

    result = Replace(result, "(", " ") 

    result = Replace(result, ")", " ") 

    result = Replace(result, "-", " ") 

    result = Replace(result, "+", " ") 

    result = Replace(result, "*", " ") 

    result = Replace(result, "/", " ") 

    result = Replace(result, "=", " ") 

    result = Replace(result, ",", " ") 

    Dim cells() As String 

    cells = Split(Trim(result), " ") 

    For j = 0 To UBound(cells) 

    Range(cells(j)).Interior.Color = lcolor 

    Next j 

End If 

Next cell 

End Sub 
相关问题