AddChart2 VBA 宏中的数字代表什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27889579/
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
What does the number in the AddChart2 VBA macro represent?
提问by OneBaseNotch
I've use my Excel 2013 to record a macro in inserting a chart, a column-clustered chart in my case. In the view code option, it shows me a line of code as below:
我已经使用我的 Excel 2013 在插入图表时记录了一个宏,在我的例子中是一个列簇图表。在查看代码选项中,它向我显示了一行代码,如下所示:
ActiveSheet.Shapes.Addchart2(286,xl3DColumnClustered).Select
Please help me as I cannot understand what does the number 286 represent. I know the syntax of Addchart2 is:
请帮助我,因为我无法理解数字 286 代表什么。我知道 Addchart2 的语法是:
expression.AddChart2(Style,XlChartType,Left,Top,Width,Height,NewLayout)
If I change the "286" to "285", the chart appears with a blue background. An error comes out if the number is 100.
如果我将“286”更改为“285”,图表将显示为蓝色背景。如果数字为 100,则会出现错误。
Can anyone kindly tell me what does the number represent?
谁能告诉我这个数字代表什么?
回答by EEM
One can also provide only the ChartType and the application will use the default style.
也可以仅提供 ChartType,应用程序将使用默认样式。
Set oShp = ActiveSheet.Shapes.AddChart2(XlChartType:=xl3DColumnClustered)
oShp.Chart.SetSourceData Source:=RngDta
This picture shows the default ChartStyle for all ChartTypes (excluding StockHLC and StockVOHLC)
此图显示了所有 ChartType(不包括 StockHLC 和 StockVOHLC)的默认 ChartStyle
回答by Pillgram
This won't directly answer your question, but it will help you figure out what is going on.
这不会直接回答您的问题,但会帮助您弄清楚发生了什么。
This is pure conjecture on my part, but I would guess it's an undocumented bitfield. As you may know a bit field is just a way to use a number. So image we have a Byte variable which can be 8 bits (or flags). So in a byte we can store up to 8 values.
这纯粹是我的猜测,但我猜这是一个未记录的位域。您可能知道位域只是使用数字的一种方式。所以图像我们有一个 Byte 变量,它可以是 8 位(或标志)。所以在一个字节中我们最多可以存储 8 个值。
Example: We have field called "DaysOpen" bits 1-7 mean the store is open on that day of the week. (We'll ignore the 8th bit.) So if the store is open M-F that would be binary 0111 1100.
示例:我们有名为“DaysOpen”的字段,第 1-7 位表示商店在一周中的那一天营业。(我们将忽略第 8 位。)所以如果商店是开放的 MF 那将是二进制 0111 1100。
Then you just convert that number to decimal and we see that it's 124.
然后你只需将该数字转换为十进制,我们就会看到它是 124。
That variable is a Variant so it could be anything from a Byte to Long meaning it could be storing up to 64 different flags.
该变量是一个 Variant,因此它可以是从 Byte 到 Long 的任何内容,这意味着它可以存储多达 64 个不同的标志。
As a side note (if you are interested) you can use bit fields like so:
作为旁注(如果您有兴趣),您可以像这样使用位字段:
Option Explicit
Public Enum DayFlags
'Notice these are power of 2.
dfSunday = 1
dfMonday = 2
dfTuesday = 4
dfWednesday = 8
dfThursday = 16
dfFriday = 32
dfSaturday = 64
End Enum
Sub Example()
Dim openHours As DayFlags
'Set the flags:
openHours = dfMonday Or dfTuesday Or dfThursday
'See the binary?
MsgBox Right$("00000000" & Excel.WorksheetFunction.Dec2Bin(openHours), 8)
'Notice the order is right to left. This is call endianness.
'You can check for a specific flag like this:
MsgBox IsOpenOnDay(openHours, dfMonday) & vbNewLine & IsOpenOnDay(openHours, dfFriday)
'You can add a flag like this:
openHours = openHours Or dfFriday
MsgBox IsOpenOnDay(openHours, dfMonday) & vbNewLine & IsOpenOnDay(openHours, dfFriday)
'You can remove a flag like this:
openHours = openHours Xor dfFriday
MsgBox IsOpenOnDay(openHours, dfMonday) & vbNewLine & IsOpenOnDay(openHours, dfFriday)
End Sub
Private Function IsOpenOnDay(ByVal openHours As DayFlags, ByVal day As DayFlags) As Boolean
IsOpenOnDay = ((openHours And day) = day)
End Function
回答by Anarach
Well , I had the same situation once, and those are basically chart styles. I tried to figure out the exact numbering but then i realized that recording was a much easier way of knowing the style numbers just as you have done here.
嗯,我也遇到过同样的情况,基本上都是图表样式。我试图找出确切的编号,但后来我意识到录音是一种更简单的方式来了解样式编号,就像您在此处所做的那样。
To answer you question, record macros to know which style you want to implement in your macros.
要回答您的问题,请记录宏以了解您要在宏中实现哪种样式。