如何为excel列指定渐变颜色?

时间:2013-04-15 00:05:28

标签: excel

我需要为Excel列中的单元格指定不同的颜色,这样第一个单元格为白色,第二个单元格稍暗,第三个单元格比前一个单元格更暗等。这是指向.png的链接档案:https://dl.dropboxusercontent.com/u/41007907/gradientColumn.png

我怎么能快速做到这一点?有快捷命令吗?

1 个答案:

答案 0 :(得分:7)

如果您正在寻找VBA解决方案,请使用单元格的.Interior.TintAndShade属性。

这是一个快速宏,您可以使用它根据列中的单元格数计算渐变填充。这应该应用偶数梯度,例如:

gradient fill cells

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.

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

Set allCells = Range("A1:A10")

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

Next


End Sub

已编辑以从浅色到深色填充渐变。如果您喜欢从黑暗到光明,那么请执行:

allCells(c).Interior.TintAndShade = _
        (c-1) / allCells.Cells.Count