表单控制在代码中使用.shapes和使用.buttons的区别?

我已经在这个macros中放了几个button,一切都很好,看起来不错,但是我有一个关于代码的问题。 正如你可以在下面看到的,在这段代码中,当使用With Activesheet ,代码使用.Shapes.BUTTONS 。 它看起来像.Shapes有更多的关于.Shapesbutton后的大小? 和.BUTTONS有更多的关系,看起来像什么,它做什么?

我只是想知道这些差异是什么,因为在一个点上,我使用。 .BUTTONS来添加button,然后我使用.shapes来改变button的大小。 但是接下来我使用.Shapes来告诉它什么时候点击OnAction而不是.BUTTONS 。 这些属性究竟有什么不同? (或者不pipe它叫什么,我都不确定)。看起来他们似乎是交织在一起的,我不太确定使用什么和什么时候使用。

 With Activesheet .BUTTONS.Add(53.25, 36.75, 190.5, 41.25).Select .Shapes("Button 6").IncrementLeft 222.75 .Shapes("Button 6").IncrementTop 147 .BUTTONS("Button 6").Text = "Mexmo MFG" .BUTTONS("Button 10").Text = "10" .BUTTONS("Button 10").name = "Button 10" .BUTTONS("Button 11").Text = "11" .BUTTONS("Button 11").name = "Button 11" .BUTTONS("Button 12").Text = "12" .BUTTONS("Button 12").name = "Button 12" .Shapes("Atlanta MFG").OnAction = "Atlanta" .Shapes("Denver MFG").OnAction = "Denver" .Shapes("Jackson MFG").OnAction = "Jackson" .Shapes("Louisville MFG").OnAction = "Louisville" End With 

有些属性只有可用的,只能使用Shape对象进行调整。 这就是为什么你的代码有ButtonsShapes集合。

大多数从Shapesinheritance的对象通常公开ShapeRange属性,该属性返回Shape对象。 你可以简单地使用它并访问所有相关的属性。


例:

 Sub test() With ActiveSheet .Buttons.Add(53.25, 36.75, 190.5, 41.25).Select .Shapes("Button 6").IncrementLeft 222.75 '/ This works same as Shapes("Button 6") .Buttons("Button 6").ShapeRange.IncrementTop 147 .Shapes("Button 6").IncrementTop 147 End With End Sub 

扩展@ cyoashu的答案,值得注意的是.Buttons.Add()方法返回添加的button ,所以你可以直接在你的With块中使用它:

 Sub test() With ActiveSheet.Buttons.Add(53.25, 36.75, 190.5, 41.25) .Name = "Blah" .Text = "Blah" .OnAction = "DoSomething" With .ShapeRange .IncrementLeft 222.75 .IncrementTop 147 .IncrementTop 147 End With End With End Sub 

…这使您不必知道任何关于刚刚添加的button的名称。