Skip i'r prif gynnwys

Sut i ychwanegu botwm arfer at y ddewislen clicio ar y dde / cyd-destun yn Excel?

Fel isod sgrinluniau a ddangosir, mae llawer o bobl yn tueddu i ychwanegu eu swyddogaethau a ddefnyddir yn aml at ddewislen clic dde Excel er mwyn defnyddio'r swyddogaethau hyn yn gyflym gyda dim ond un clic yn y dyfodol. Mae'r erthygl hon yn sôn am ychwanegu botwm cod arferiad i'r ddewislen clic dde yn Excel.

Ychwanegwch botwm arfer i'r ddewislen cyd-destun yn Excel gyda chod VBA


Ychwanegwch botwm arfer i'r ddewislen cyd-destun yn Excel gyda chod VBA

Gan dybio eich bod wedi creu sgript VBA o'r enw MyMacro yn eich Excel, ac yn awr mae angen ichi ychwanegu'r swyddogaeth hon at y ddewislen cyd-destun ar gyfer rhedeg y cod dim ond trwy ei glicio. Gwnewch fel a ganlyn.

1. Gwasgwch y Alt + F11 allweddi ar yr un pryd i agor y Microsoft Visual Basic ar gyfer Ceisiadau ffenestr.

2. Yn y Microsoft Visual Basic ar gyfer Ceisiadau ffenestr, cliciwch ddwywaith Llyfr Gwaith hwn yn y chwith Prosiect cwarel. Ac yna copïwch a gludwch y cod VBA isod i'r Y Llyfr Gwaith hwn (Cod) ffenestr.

Cod VBA: Ychwanegu botwm arfer i'r ddewislen cyd-destun yn Excel

Private Sub Workbook_Deactivate()
    On Error Resume Next
        With Application
            .CommandBars("Cell").Controls("MyMacro").Delete
        End With
    On Error GoTo 0
End Sub

Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    Dim cmdBtn As CommandBarButton
    On Error Resume Next
        With Application
            .CommandBars("Cell").Controls("MyMacro").Delete
            Set cmdBtn = .CommandBars("Cell").Controls.Add(Temporary:=True)
        End With

        With cmdBtn
           .Caption = "MyMacro"
           .Style = msoButtonCaption
           .OnAction = "MyMacro"
        End With
    On Error GoTo 0
End Sub

Nodyn: Yn y cod, disodli'r cyfan “FyMacro” gyda'r enw Macro rydych chi wedi'i greu yn eich Excel.

3. Gwasgwch y Alt + Q allweddi i adael y Microsoft Visual Basic ar gyfer Ceisiadau ffenestr.

Nawr eich bod yn dychwelyd i'r rhyngwyneb Excel, ar ôl clicio ar dde ar gell, fe welwch fod y botwm newydd gyda'r enw penodedig yn cael ei ychwanegu i'r ddewislen clicio ar y dde fel y dangosir isod y screenshot. Bydd clicio'r botwm yn sbarduno'r Macro cyfatebol ar unwaith.


Erthyglau perthnasol:

Offer Cynhyrchiant Swyddfa Gorau

🤖 Kutools AI Aide: Chwyldro dadansoddi data yn seiliedig ar: Cyflawniad Deallus   |  Cynhyrchu Cod  |  Creu Fformiwlâu Personol  |  Dadansoddi Data a Chynhyrchu Siartiau  |  Invoke Swyddogaethau Kutools...
Nodweddion Poblogaidd: Darganfod, Amlygu neu Adnabod Dyblygiadau   |  Dileu Rhesi Gwag   |  Cyfuno Colofnau neu Gelloedd heb Colli Data   |   Rownd heb Fformiwla ...
Super-edrych: VLookup Meini Prawf Lluosog    VLookup Gwerth Lluosog  |   VLookup Ar Draws Taflenni Lluosog   |   Edrych Niwlog ....
Rhestr gwympo Uwch: Creu Rhestr Gollwng yn Gyflym   |  Rhestr Gollwng Dibynnol   |  Rhestr Gollwng Aml-ddewis ....
Rheolwr Colofn: Ychwanegu Nifer Penodol o Golofnau  |  Symud Colofnau  |  Toglo Statws Gwelededd Colofnau Cudd  |  Cymharwch Ystodau a Cholofnau ...
Nodweddion dan Sylw: Ffocws ar y Grid   |  Golwg Dylunio   |   Bar Fformiwla Mawr    Rheolwr Llyfr Gwaith a Thaflen   |  Llyfrgell Adnoddau (Testun Auto)   |  Dewiswr Dyddiad   |  Cyfuno Taflenni Gwaith   |  Amgryptio/Dadgryptio Celloedd    Anfon E-byst trwy Restr   |  Hidlo Super   |   Hidlo Arbennig (hidlo mewn print trwm/italig/strikethrough...) ...
15 Set Offer Gorau12 Testun offer (Ychwanegu Testun, Dileu Cymeriadau,...)   |   50 + Siart Mathau (Siart Gantt,...)   |   40+ Ymarferol Fformiwlâu (Cyfrifwch oedran yn seiliedig ar ben-blwydd,...)   |   19 mewnosod offer (Mewnosod Cod QR, Mewnosod Llun o'r Llwybr,...)   |   12 Trosi offer (Rhifau i Eiriau, Trosi arian cyfred,...)   |   7 Uno a Hollti offer (Rhesi Cyfuno Uwch, Celloedd Hollt,...)   |   ... a mwy

Supercharge Eich Sgiliau Excel gyda Kutools ar gyfer Excel, a Phrofiad Effeithlonrwydd Fel Erioed Erioed. Kutools ar gyfer Excel Yn Cynnig Dros 300 o Nodweddion Uwch i Hybu Cynhyrchiant ac Arbed Amser.  Cliciwch Yma i Gael Y Nodwedd Sydd Ei Angen Y Mwyaf...

Disgrifiad


Mae Office Tab yn dod â rhyngwyneb Tabbed i Office, ac yn Gwneud Eich Gwaith yn Haws o lawer

  • Galluogi golygu a darllen tabbed yn Word, Excel, PowerPoint, Cyhoeddwr, Mynediad, Visio a Phrosiect.
  • Agor a chreu dogfennau lluosog mewn tabiau newydd o'r un ffenestr, yn hytrach nag mewn ffenestri newydd.
  • Yn cynyddu eich cynhyrchiant 50%, ac yn lleihau cannoedd o gliciau llygoden i chi bob dydd!
Comments (15)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Zdravím dokázal byste mi prosím někdo poradit s podobným problémem? Po posledním updatu Office přibyla v excelu po stisknutí pravého tlačítka nad kopírovat a vyjmout volba hledat v nabídkách která mi tam vadí a chtěl bych jí odstranit. Dá se to udělat i obráceně a nějakou funkci odstranit?

děkuji
This comment was minimized by the moderator on the site
Hi Tomáš,
I have not encountered this situation. Can you attach a screenshot to describe the problem you encountered more clearly.
This comment was minimized by the moderator on the site
Bonjour,

Je déterre le sujet car j'ai un soucis, j'ai récupérer un fichier qui m'a remplacé le menu du click droit et je ne sais pas comment revenir au menu par défaut...
Ça a modifier le menu pour n'importe quel fichier excel. J'utilise excel 2016.

Je précise que le fichier coupable a un mot de passe pour accéder à son code VBA. Mot de passe qu’évidemment je ne possède pas..

Merci d'avance pour votre aide !!
This comment was minimized by the moderator on the site
Hi Olivier,
Sorry I don't quite understand what you mean. Do you mean to back to the normal right-clicking menu?
This comment was minimized by the moderator on the site
Thanks
Does that working for all workbooks even after restarting Excel, or only for the selected workbook which saved the macro in?
This comment was minimized by the moderator on the site
Размести код в отдельном модуле, для того чтобы это работало для всех документов
This comment was minimized by the moderator on the site
Hi,
It only works for the workbook which save the macro in. Thank you for your comment.
This comment was minimized by the moderator on the site
Add VBA button to context menu works well. Thanks. How do I add more buttons ? (not good at VBA, hence the question).
This comment was minimized by the moderator on the site
Hi Vic,
The following VBA code can help you solve the problem, please have a try.

Private Sub Workbook_Deactivate()
Dim xArrB As Variant
Dim xFNum As Integer
Dim xStr As String
On Error Resume Next
With Application
xArrB = Array("MyMacro01", "MyMacro02", "MyMacro03")
For xFNum = 0 To UBound(xArrB)
xStr = xArrB(xFNum)
.CommandBars("Cell").Controls(xStr).Delete
Next xFNum
End With
On Error GoTo 0
End Sub

Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Dim cmdBtn As CommandBarButton
Dim xArrB As Variant
Dim xFNum As Integer
Dim xStr As String
On Error Resume Next
xArrB = Array("MyMacro01", "MyMacro02", "MyMacro03")
For xFNum = 0 To UBound(xArrB)
xStr = xArrB(xFNum)
With Application
.CommandBars("Cell").Controls(xStr).Delete
Set cmdBtn = .CommandBars("Cell").Controls.Add(Temporary:=True)
End With
With cmdBtn
.Caption = xStr
.Style = msoButtonCaption
.OnAction = xStr
End With
Next xFNum
On Error GoTo 0
End Sub
This comment was minimized by the moderator on the site
This doesn't work for me, I have done exactly the same as the above macro with only one menu, Does it work for anyone else or am I doing something wrong?

Thanks
This comment was minimized by the moderator on the site
Don t work with my Excel 2013
This comment was minimized by the moderator on the site
Hi Yves,I tested this code on Excel 2013 and it works fin. Did you get any error prompt? I need to know more specific about your issue
This comment was minimized by the moderator on the site
Thanks for your answerBut doesn't work on a table. On normal cell is work perfectly.
This comment was minimized by the moderator on the site
Hi yves,The problem is clear now, and I need time to fix it. Please wait. Thank you for your feedback.
This comment was minimized by the moderator on the site
Thanks a lot
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations