当前位置:   article > 正文

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

excel宏菜单范例

主要介绍 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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/860813
推荐阅读
相关标签
  

闽ICP备14008679号