【问题标题】:Excel VBA - Highlight cell that have been summedExcel VBA - 突出显示已求和的单元格
【发布时间】:2015-06-28 23:13:29
【问题描述】:

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

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

非常感谢任何帮助。

【问题讨论】:

  • 所有的“总和细胞”都是一系列细胞+细胞+细胞(或更多)吗?您可以编写一个宏,将 splits 写入 + 符号上的数组中(我们称之为 summedCells),然后为每个数组元素打开背景颜色。 Range(summedCells(i)).Interior.ColorIndex = 5.

标签: excel sum vba


【解决方案1】:

这可能会给你一个想法。以下代码会将所选单元格的所有直接先例着色为浅绿色。例如,在 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

修改代码很容易,这样会弹出一个输入框要求用户选择颜色

【讨论】:

    【解决方案2】:

    如果以下 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-18
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      • 2017-10-29
      • 1970-01-01
      • 1970-01-01
      • 2015-01-26
      相关资源
      最近更新 更多