Excel: setting the color of a cell to another

耗尽温柔 提交于 2019-12-24 03:13:21

问题


How do I determine the color of a cell equal to another, example: A4 is then C2 is cyan color cyan. A2 is then C2 orange color is orange.


回答1:


There is no excel-formula to get you the color of a cell, nor is there one, to set the color of a different one.

However, you can make a function to get the color of a specific cell - or, like in my example, the color of the function-calling cell:

Public Function GetColor()
    Dim rng As Range

    If TypeName(Application.Caller) = "Range" Then
        Set rng = Application.Caller
    End If
    GetColor = rng.Cells.Interior.Color
End Function

Now you might, think, ok then I modify this, just to SET the color too. But no - does not work that way. In order to change a cells color, you would have to use Worksheet_Change event and setup each cell to the long value inside them as their color.

Target.Interior.Color = Target.Value

Would be the line for that, when using Worksheet_Change.

You can of course use ColorIndex as well - just adapt accordingly.




回答2:


here is some C# code, maybe it could be helpful to you:

xlSheet.Range["A10", "A10"].Interior.Color = ColorTranslator.ToOle(System.Drawing.Color.Cyan);
xlSheet.Range["C10", "C10"].Interior.Color = xlSheet.Range["A10", "A10"].Interior.Color;


来源:https://stackoverflow.com/questions/13360600/excel-setting-the-color-of-a-cell-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!