使用 vba 宏以编程方式将彩色垂直带添加到 Excel 图表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8200685/
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
adding colored vertical band to excel chart programatically using vba macro
提问by Omran
I am trying to add colored vertical band to a chart in excel programatically using vba macro.
我正在尝试使用 vba 宏以编程方式在 excel 中的图表中添加彩色垂直带。
I have a chart that contains some numbers x-axis from 1 - 50 and I want to add a colored vertical band from 1 - 10 and another one from 10 - 20 with different color and so on.
我有一个图表,其中包含 1 - 50 的一些数字 x 轴,我想添加一个 1 - 10 的彩色垂直带和另一个 10 - 20 的不同颜色的垂直带等等。
Any help or clue
任何帮助或线索
回答by Rachel Hettinger
This solution uses an image for the plot area background.
此解决方案使用图像作为绘图区域背景。
This line of code sets the background image. Be sure to update the sheet reference, chart name, and file name as necessary.
这行代码设置背景图像。请务必根据需要更新工作表参考、图表名称和文件名。
ActiveSheet.ChartObjects("Chart 1").Chart.PlotArea.Format.Fill.UserPicture "C:\background.png"
I use MS Paint to create the various background images required. Here are the steps:
我使用 MS Paint 创建所需的各种背景图像。以下是步骤:
Create a new file and set the attributes to be 1 pixel high and X pixels wide where X is the number of bands you need.
Set the zoom to as large as possible.
Using the Pencil tool, color each pixel as you would like the bands to appear.
Resize the image by 500% horizontal and vertical; do this 2-3 times to get an image big enough so it doesn't have to stretch in the chart. (Note: leaving it small produces blending of the colored bars which can be desirable.)
Save the file.
创建一个新文件并将属性设置为 1 像素高和 X 像素宽,其中 X 是您需要的波段数。
将缩放设置为尽可能大。
使用铅笔工具,按照您希望条带出现的方式为每个像素着色。
将图像水平和垂直调整 500%;这样做 2-3 次以获得足够大的图像,这样它就不必在图表中拉伸。(注意:让它很小会产生可能需要的彩色条的混合。)
保存文件。
If your charts will have a variable number of points plotted, prepare several background images and then select the appropriate one in your code.
如果您的图表将绘制不同数量的点,请准备几张背景图像,然后在您的代码中选择适当的一张。