Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1640|回复: 3

[已解决]谁能帮我编写一个代码,谢谢!

[复制链接]
发表于 2012-8-23 16:34 | 显示全部楼层 |阅读模式
这个代码就是在2007版Excel中的功能区,也就是加载项中出现一个命令按钮,我点击这个按钮,就用Msgbox显示一句话就好了。
最佳答案
2012-8-23 18:34
本帖最后由 柳如烟 于 2012-8-23 18:58 编辑

  1. Sub 生成工具栏()
  2.     Dim mybar As CommandBar, ctl As CommandBarButton
  3.     Set mybar = CommandBars.Add("我的工具栏", , False, True)
  4.     mybar.Visible = True
  5.     Set ctl = mybar.Controls.Add
  6.     With ctl
  7.         .Caption = "你好"
  8.         .OnAction = "Hello"
  9.         .FaceId = 59
  10.         .Style = msoButtonIconAndCaption
  11.     End With
  12. End Sub
  13. Sub Hello()
  14.     MsgBox "您好!"
  15. End Sub
  16. Sub 删除工具栏()
  17.     On Error Resume Next
  18.     CommandBars("我的工具栏").Delete
  19. End Sub
  20. Private Sub Workbook_BeforeClose(Cancel As Boolean)
  21.     删除工具栏
  22. End Sub
  23. Private Sub Workbook_Open()
  24.     生成工具栏
  25. End Sub
复制代码
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2012-8-23 16:40 | 显示全部楼层
和2003一样,但一定要宏级别设定一下
回复

使用道具 举报

 楼主| 发表于 2012-8-23 16:55 | 显示全部楼层
yanyongxia 发表于 2012-8-23 16:40
和2003一样,但一定要宏级别设定一下

我写了,可是没反应。所以想找个人写一下。我测试一下,看看我的有啥问题。
回复

使用道具 举报

发表于 2012-8-23 18:34 | 显示全部楼层    本楼为最佳答案   
本帖最后由 柳如烟 于 2012-8-23 18:58 编辑

  1. Sub 生成工具栏()
  2.     Dim mybar As CommandBar, ctl As CommandBarButton
  3.     Set mybar = CommandBars.Add("我的工具栏", , False, True)
  4.     mybar.Visible = True
  5.     Set ctl = mybar.Controls.Add
  6.     With ctl
  7.         .Caption = "你好"
  8.         .OnAction = "Hello"
  9.         .FaceId = 59
  10.         .Style = msoButtonIconAndCaption
  11.     End With
  12. End Sub
  13. Sub Hello()
  14.     MsgBox "您好!"
  15. End Sub
  16. Sub 删除工具栏()
  17.     On Error Resume Next
  18.     CommandBars("我的工具栏").Delete
  19. End Sub
  20. Private Sub Workbook_BeforeClose(Cancel As Boolean)
  21.     删除工具栏
  22. End Sub
  23. Private Sub Workbook_Open()
  24.     生成工具栏
  25. End Sub
复制代码
20120823.gif

示例文件.rar

13.85 KB, 下载次数: 5

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-21 05:56 , Processed in 0.252814 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表