Excel(VBA)自定义右键单击菜单以启动宏(示例代码)

主要介绍 Excel(VBA)自定义右键单击菜单以启动宏(示例代码)以及相关的经验技巧

  1. THISWORKBOOK (paste into ThisWorkbook, macros that open and closed menus when launching and closing spreadsheet)

  2. Private Sub Workbook_Open()

  3. MsgBox "You can right-click any worksheet cell" & vbCrLf & _

  4. "to see and / or run your workbook's macros.", 64, "A tip:"

  5. Run "RightClickReset"

  6. Run "MakeMenu"

  7. End Sub

  8. Private Sub Workbook_Activate()

  9. Run "RightClickReset"

  10. Run "MakeMenu"

  11. End Sub

  12. Private Sub Workbook_Deactivate()

  13. Run "RightClickReset"

  14. End Sub

  15. Private Sub Workbook_BeforeClose(Cancel As Boolean)

  16. Run "RightClickReset"

  17. ThisWorkbook.Save

  18. End Sub

  19. 'DEMONSTRATIONMACROS (paste into module DemonstrationMacros, macros you want to launch from the custom menu, these are examples)

  20. Sub Macro1()

  21. MsgBox "This is Macro1.", 64, "Test 1"

  22. End Sub

  23. Private Sub Macro2()

  24. MsgBox "This is Macro2.", 64, "Test 2"

  25. End Sub

  26. Sub Macro3()

  27. MsgBox "This is Macro3.", 64, "Test 3"

  28. End Sub

  29. 'MAINTENANCEMACROS (paste into module MaintenanceMacros, macros for creation and running of custom menu)

  30. Private Sub RightClickReset()

  31. On Error Resume Next

  32. CommandBars("Cell").Controls("Macro List").Delete

  33. Err.Clear

  34. CommandBars("Cell").Reset

  35. End Sub

  36. Private Sub MakeMenu()

  37. Run "RightClickReset"

  38. Dim objCntr As CommandBarControl, objBtn As CommandBarButton

  39. Dim strMacroName$

  40. Set objCntr = _

  41. Application.CommandBars("Cell").Controls.Add(msoControlPopup, before:=1)

  42. objCntr.Caption = "Macro List"

  43. Application.CommandBars("Cell").Controls(2).BeginGroup = True

  44. Dim intLine%, intArgumentStart%, strLine$, objComponent As Object

  45. For Each objComponent In ActiveWorkbook.VBProject.VBComponents

  46. If objComponent.Type = 1 Then

  47. For intLine = 1 To objComponent.CodeModule.CountOfLines

  48. strLine = objComponent.CodeModule.Lines(intLine, 1)

  49. strLine = Trim$(strLine) 'Remove indented spaces

  50. If Left$(strLine, 3) = "Sub" Or Left$(strLine, 11) = "Private Sub" Then

  51. intArgumentStart = InStr(strLine, "()")

  52. If intArgumentStart > 0 Then

  53. If Left$(strLine, 3) = "Sub" Then

  54. strMacroName = Trim(Mid$(strLine, 4, intArgumentStart - 4))

  55. Else

  56. strMacroName = Trim(Mid$(strLine, 12, intArgumentStart - 12))

  57. End If

  58. If strMacroName <> "RightClickReset" And strMacroName <> "MakeMenu" Then

  59. If strMacroName <> "MacroChosen" Then

  60. Set objBtn = objCntr.Controls.Add

  61. With objBtn

  62. .Caption = strMacroName

  63. .Style = msoButtonIconAndCaption

  64. .OnAction = "MacroChosen"

  65. .FaceId = 643

  66. End With

  67. End If

  68. End If

  69. End If

  70. End If

  71. Next intLine

  72. End If

  73. Next objComponent

  74. End Sub

  75. Private Sub MacroChosen()

  76. With Application

  77. Run .CommandBars("Cell").Controls(1).Controls(.Caller(1)).Caption

  78. End With

  79. End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值