vba 在excel中为单个单元格添加两种颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27288268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 05:24:26  来源:igfitidea点击:

Adding two colours to a single cell in excel

excelexcel-vbavba

提问by Alwyn Miranda

Is it possible to fill two colors in a single cell?

是否可以在一个单元格中填充两种颜色?

(for ex: half of A1 cell with red and other half of A11 cell with green color)

(例如:A1 单元格的一半为红色,A11 单元格的另一半为绿色)

回答by Hebrus

This schould be possible using

这应该可以使用

Format Cell > Fill Tab > Fill Effects

设置单元格格式 > 填充选项卡 > 填充效果

select

选择

Two Colors (choose your colors)

两种颜色(选择你的颜色)

回答by Dane I

Use Colour Gradients, the coding below is taken from this website, it will hopefully be of assistance:

使用颜色渐变,以下代码取自本网站,希望对您有所帮助:

http://software-solutions-online.com/2014/04/09/excel-vba-gradients-colors/

http://software-solutions-online.com/2014/04/09/excel-vba-gradients-colors/

Sub Example1_a()
Dim objColorStop As ColorStop
Dim lngColor1 As Long

'creates the gradient in cell A1 
Range("A1").Interior.Pattern = xlPatternLinearGradient
'changes its orientation 
Range("A1").Interior.Gradient.Degree = 90
'gets the color code for the second colorstop object 
lngColor1 = Range("A1").Interior.Gradient.ColorStops(2).Color
'clears the previous colostop objects 
Range("A1").Interior.Gradient.ColorStops.Clear
'creates a colorstop object with the position 0 
Set objColorStop = Range("A1").Interior.Gradient.ColorStops.Add(0)
'changes its color to green 
objColorStop.Color = vbGreen
'creates a colorstop object with the position 1 
Set objColorStop = Range("A1").Interior.Gradient.ColorStops.Add(1)
'changes its color to red 
objColorStop.Color = lngColor1
End Sub 

回答by zi2000

some answers below suggest gradient fills, but these don't do a clear cut between two colours.

下面的一些答案建议使用渐变填充,但这些在两种颜色之间并没有明确的划分。

after working with the "clear cut" problem I found this solution how to "cut" the color in gradients

在解决了“清晰切割”问题后,我找到了这个解决方案如何“切割”渐变中的颜色

example:

例子:

.Gradient.ColorStops.Add(0)   .color = color1
.Gradient.ColorStops.Add(0.5) .color = color1
.Gradient.ColorStops.Add(0.51) .color = color2
.Gradient.ColorStops.Add(1)    .color = color2

this will show color1 in the first half of the cell, and color2 in the second half

这将在单元格的前半部分显示 color1,在后半部分显示 color2

of course you can do the "cut line" where you need it e.g. position 0.3 | 0.31

当然你可以在你需要的地方做“切割线”,例如位置 0.3 | 0.31