在单元格中放置命令按钮 MS Excel vba
我想通过 VBA 代码在单元格中放置一个命令按钮。说位置B3。我为此目的使用了宏记录器,但它为我提供了按钮的顶部底部值。我不希望这样,因为如果我将代码带到具有其他屏幕分辨率的其他计算机上,代码将会失败。单元格位置(示例 B3)将是绝对位置。
你能建议我一种方法来做到这一点吗?
PS 它是一个 activeX 按钮
谢谢
I want to place a command button in a cell though VBA code. Say position B3. I used macro recorder for this purpose but it gives me the top bottom values of the button. I don't want that cuz if I take my code to some other computer with another screen resolution, the code will fail. A cell position (example B3) will be an absolute position.
Can you suggest me a way to do this.
P.S Its an activeX button
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能将任何对象放置在单元格“中”,只能放置在单元格上方。您可以将按钮的 Left 和 Top 属性设置为单元格的 Left/Top。
You can't place any object "in" a cell, only over it. You can set the button's Left and Top properties to the Cell's Left/Top.
“在单元格中放置按钮”的替代方案
使用工作表事件直接选择单元格执行代码:
Worksheet_SelectionChange
。将代码放置在特定工作表的模块中。根据需要对单元格进行颜色/边框/文本设置。任何计算机或屏幕上的单元都是单元。当用户单击简短的标签/描述时,我使用它来引用加载到用户表单中的帮助,从帮助表上查找。使用单元格/按钮可避免 IT 部门对 Active-X 对象的投诉。需要考虑的事项,示例代码如下:
Select Case
,即使您只有一个单元格/按钮。这简化了添加单元格/按钮的路径,稍后Target.Address
返回整个范围,而不仅仅是一个单元格。如果您的Select Case
引用Target
左上角单元格的地址,则可以避免此问题。Target.Cells(1,1).Address
MergeArea.Address
(MergeArea
不起作用 ];它返回单元格所在的合并范围。在合并单元格上[
仅适用于单个单元格 .jpg" rel="nofollow noreferrer">
An Alternative to "putting a button in a cell"
Make a cell selection execute code, directly, using the worksheet event:
Worksheet_SelectionChange
. Place the code in the specific sheet's module. Color/Border/Text the cell as you please. A cell-is-a-cell on any computer or screen. I use this to reference help loaded into a userForm, from a look up on a help sheet, when the user clicks on a short label/description. Using cell/buttons avoids Active-X objects complaints from IT.Things to think on, with the sample code, following:
Select Case
in your code, even if you have one cell/button. This eases the path to adding cell/buttons, laterTarget.Address
for the merged area returns the full range, not just one cell. If yourSelect Case
refers to the address of theTarget
's top-left cell, you avoid this problem.Target.Cells(1,1).Address
MergeArea.Address
(MergeArea
will not work on merged cells [only works on single cells]; it returns the merged range within which a cell lives.*Sample Code*