如何强制excel细胞改变颜色EXPONENTIALLY?

我需要写一个这样的macros:我用黑色填充A1。 然后当我运行macros时,A2应该稍微轻一点,A3甚至更轻…等,直到A20是白色的。 “F5”单元值应该控制梯度指数的大小。 当前的代码按比例改变颜色。 当我改变“F5”的值(例如从1到0.7)时,会发生的是,这20个单元(“A1:A20”)中的所有单元变得等同地变暗。 最后一个单元格A20不再是白色了。

然而,我需要我的拳头细胞“A1”是黑色的,最后一个细胞“A20”是白色的, 不pipe怎样…细胞的颜色分布应该是指数,即A1和A2之间的黑暗差异应该是A3和A2之间的黑暗差异的两倍(如果“F5”== 2)…

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 

我不知道,我应该实现什么样的function,以便颜色的变化是指数的,因为我改变了“F5”中的variablescontrast的值? //和

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

在这里输入图像说明

你不能同时拥有“下一个单元格是白色的两倍”和“第一个单元格是黑色的,最后一个单元格是白色的”。 你正在寻找的东西叫做“伽马函数”(伽玛函数) – 从0到255之间的数字缩放比例,其中比较轻的比率取决于一个因素(有时称为伽马)。

在其基本forms中,您可以使用如下所示的内容:

 contrast = ((cellNum-1)/(numCells-1))^gamma 

现在,如果你的gamma是1,那么缩放将是线性的。 当gamma> 1时,强度将会在最后几个单元中增加得更快。 当它小于1时,前几个单元会变化很快。

我在上面假设cellNum从1到20,而numCells是20.在你使用的.TintAndShadeexpression式中,这个对.TintAndShade应该给你你正在寻找的效果。 gamma不需要是一个整数,但如果它<0,你会得到对比度> 1,这会给你奇怪的结果(全白,我想象)。

顺便说一句 – 将你的macros3重命名为更合理的( adjustContrast ),并用F5的值作为参数调用它:

 Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Target, Range("F5")) Is Nothing Then adjustContrast Target.Value End If End Sub 

 Sub adjustContrast(gamma) ... etc 

由于从您的评论中可以清楚地看到,在我原来的发帖中我还不够清楚,下面是完整的代码以及给出的结果。 注意 – 这是用于演示改变显示器上的伽马效果的代码,而不是您想要使用的确切代码(例如,我循环四列,并且有四个不同的gamma值):

 Sub applyGamma() Dim ii, jj As Integer Dim contrast As Double Dim cellColor, fontColor, fontInvColor As Long Dim allCells As Range Dim gamma As Double On Error GoTo recovery Application.ScreenUpdating = False Set allCells = [A2:A21] ' default formatting taken from cell A1 cellColor = [A1].Interior.Color fontColor = [A1].Font.Color fontInvColor = 16777215 - fontColor ' use the "inverse" color... sloppy way to always see the numbers For jj = 1 To 4 Set allCells = allCells.Offset(0, 1) gamma = Cells(1, jj + 1).Value ' pick gamma from the column header For ii = 1 To 20 ' loop over all the cells contrast = ((ii - 1) / 19) ^ gamma ' pick the contrast for this cell allCells.Cells(ii, 1).Interior.Color = cellColor allCells(ii, 1).Interior.TintAndShade = contrast If contrast > 0.5 Then allCells.Cells(ii, 1).Font.Color = fontInvColor Else allCells(ii, 1).Font.Color = fontColor Next ii ' repeat for next column: Next jj recovery: Application.ScreenUpdating = True End Sub 

在我运行代码之前,我的屏幕看起来像这样(单元格中的值是给定gamma的对比度值):

在这里输入图像描述

运行后,它看起来像这样:

在这里输入图像说明

正如你所看到的,我添加了一个额外的“function”:字体的着色被改变,以保持可见的东西。 当然,这是假定“模板单元格”(在我的例子中为A1 )在字体和填充颜色之间具有良好的对比度。

为了让它工作在指数上,你可以尝试使用下面的逻辑:

在这里输入图像说明

下面的代码与你的代码相比稍有改变:

 Sub Macro3_proposal_revers() 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("B1") cellColor = firstCell.Interior.Color contrast = Range("F5").Value Set allCells = Range("B1:B20") Dim allCellsCount! allCellsCount = allCells.Cells.Count - 1 Dim newContrast As Double For c = 1 To allCells.Cells.Count - 1 allCells(c + 1).Interior.Color = cellColor 'var 1 newContrast = (1 - 0.9 ^ (c * (1 + (c / allCellsCount))) * contrast) allCells(c + 1).Interior.TintAndShade = newContrast 'control value- to delete allCells(c + 1).Offset(0, 1).Value = allCells(c + 1).Interior.TintAndShade Next End Sub 

什么是重要的 – 看看这一行:

  newContrast = (1 - 0.9 ^ (c * (1 + (c / allCellsCount))) * contrast) 

在哪里你可以做任何你想要的,例如。 把(1 + (c / allCellsCount))改成1到2之间的数来理解逻辑的方式。 一般来说,你可以调整阴影的变化速度来操作这一行,特别是用这部分代码来操作: (c * (1 + (c / allCellsCount))