【问题标题】:Excel macro for not proportional gradient colors assigned to a range of cellsExcel 宏,用于分配给一系列单元格的不成比例的渐变颜色
【发布时间】:2013-04-22 10:14:04
【问题描述】:

我需要编写一个宏:我用紫色填充 A1。然后当我运行宏时,A2 应该更轻一点,A3 更轻......等等,直到 A20 是白色的。但是这种颜色变化不应该成比例,即“变得更亮”的单元格中的边缘颜色变化应该下降(使得 A2 比 A1 更亮,比 A3 比 A2 更亮)。 底线是:细胞应该变得更轻,但不成比例。

到目前为止,我有以下代码:

Sub Macro3()

Dim firstCell As Range 'the first cell, and the cell whose color will be used for all others.
Dim cellColor As Long 'the cell color that you will use, based on firstCell
Dim allCells As Range 'all cells in the column you want to color
Dim c As Long  'cell counter
Dim tintFactor As Double 'computed factor based on # of cells.
Dim contrast As Integer

Set firstCell = Range("A1")
cellColor = firstCell.Interior.Color
contrast = Range("F5").Value


Set allCells = Range("A1:A20")

For c = allCells.Cells.Count To 1 Step -1
    allCells(c).Interior.Color = cellColor
    allCells(c).Interior.TintAndShade = contrast * _
        (c - 1) / allCells.Cells.Count
  Next

End Sub

我尝试将整数变量Dim contrast as Integer 引入单元格“F5”,这样当我更改“F5”中的值时,颜色的边际减少会下降。但这不起作用。如何改进代码?

【问题讨论】:

  • 你真是太好了,谢谢。祝你的编码好运!

标签: excel colors gradient vba


【解决方案1】:

下面是带有tan函数的代码,结果如下图:

在 B 列中,您会发现 T&S 颜色参数之间的差异。

Sub Macro3_proposal()

Dim firstCell As Range 'the first cell, and the cell whose color will be used for all others.
Dim cellColor As Long 'the cell color that you will use, based on firstCell
Dim allCells As Range 'all cells in the column you want to color
Dim c As Long  'cell counter
Dim tintFactor As Double 'computed factor based on # of cells.
Dim contrast As Integer

Set firstCell = Range("A1")
cellColor = firstCell.Interior.Color
contrast = Range("F5").Value


Set allCells = Range("A1:A20")

Dim allCellsCount!
allCellsCount = allCells.Cells.Count - 1
For c = 1 To allCellsCount 
    allCells(c + 1).Interior.Color = cellColor

    allCells(c + 1).Value = contrast * (Tan(c / allCellsCount) / Tan(1))
    allCells(c + 1).Interior.TintAndShade = contrast * (Tan(c / allCellsCount) / Tan(1))
  Next

End Sub

【讨论】:

    【解决方案2】:

    在单元格 F5 上进行数据验证,确认其内容应介于 -1 和 1 之间,然后更改代码,使对比度不是整数,而是 Double(浮点):

    Sub Macro3()
    
        Dim firstCell As Range 'the first cell, and the cell whose color will be used for all others.
        Dim cellColor As Long 'the cell color that you will use, based on firstCell
        Dim allCells As Range 'all cells in the column you want to color
        Dim c As Long  'cell counter
        Dim tintFactor As Double 'computed factor based on # of cells.
        Dim contrast As Double 'double precision factor for changing the contrast 0= none higher is more
    
        Set firstCell = Range("A1")
        cellColor = firstCell.Interior.Color
        contrast = Range("F5").Value
    
    
        Set allCells = Range("A1:A20")
    
        For c = allCells.Cells.Count To 1 Step -1
            allCells(c).Interior.Color = cellColor
            allCells(c).Interior.TintAndShade = _
                contrast * (c - 1) / (allCells.Cells.Count -1)
    
        Next
    
    End Sub
    

    值 0 是所有相同的颜色,最大为 1 将增加底部的白色,减少到 -1 将增加底部的黑色。该值不能超过 -1 或 1,因此这些是您的 Cell F5 限制。

    在自动更新您漂亮的颜色丝带旁边,将Worksheet_Change 子添加到您的 VBA:

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("F5")) Is Nothing Then
            Call Macro3
        End If
    End Sub
    

    一切就绪!

    【讨论】:

    • 谢谢。但这并不能完全解决问题。当我更改“F5”中的值(例如从 1 到 0.7)时,会发生什么,这 20 个单元格(“A1:A20”)中的所有单元格都变暗了。最后一个单元格 A20 不再是白色的了。但是,无论如何,我需要我的第一个单元格“A1”为紫色,最后一个单元格“A20”为白色......而且,中间单元格(即 A2:A19)的颜色分布不应成比例.如果这个等式有意义,我的意思是颜色应该是:(A1-A2)>(A2-A3)>(A3-A4)>(A4-A5)>....>(A18-A19)>(A19-A20);
    • 即A2 比 A1“白”的程度应该大于 A3 比 A2“白”的程度。
    • 当我运行它时,它工作得非常好。您是否实际上将代码更改为我发布的内容,如果没有,请先这样做?此外,如果您不成比例地想要它,您应该精确定义您想要的东西,如果您对此感到困惑,请发布一个带有该问题的新问题(否则 1 个问题将成为一个永无止境的问题,无法帮助社区中的任何人获得明确的参考)跨度>
    • 如果您希望最终的单元格是真正真正的白色,请将 TintAndShade 更改为 contrast * (c - 1) / (allCells.Cells.Count-1),这样您就可以在全彩色和全白之间获得 19 个间隔,因此总共有 20 种颜色!我相应地更改了答案中的脚本
    • 对于“不成比例”的部分,请注意我不是不愿意提供帮助,但是这条路径有无限的轨迹(指数、二次、圆形、正弦、余弦、切线、离散规则、组合其中,甚至其他)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 2015-01-28
    • 2013-06-04
    • 2013-11-29
    相关资源
    最近更新 更多