Loncat ke daftar isi utama

Bagaimana cara menambahkan tombol kustom ke menu klik kanan / konteks di Excel?

Seperti screenshot di bawah ini yang ditampilkan, banyak orang cenderung menambahkan fungsi yang sering digunakan ke menu klik kanan Excel untuk menggunakan fungsi ini dengan cepat hanya dengan satu klik di masa mendatang. Artikel ini berbicara tentang menambahkan tombol kode kustom ke menu klik kanan di Excel.

Tambahkan tombol kustom ke menu konteks di Excel dengan kode VBA


Tambahkan tombol kustom ke menu konteks di Excel dengan kode VBA

Misalkan Anda telah membuat skrip VBA bernama MyMacro di Excel Anda, dan sekarang Anda perlu menambahkan fungsi ini ke menu konteks untuk menjalankan kode hanya dengan mengkliknya. Silakan lakukan sebagai berikut.

1. tekan lain + F11 tombol secara bersamaan untuk membuka Microsoft Visual Basic untuk Aplikasi jendela.

2. Dalam Microsoft Visual Basic untuk Aplikasi jendela, klik dua kali Buku Kerja Ini di kiri Project panel. Dan kemudian salin dan tempel kode VBA di bawah ini ke file ThisWorkbook (Kode) jendela.

Kode VBA: Tambahkan tombol kustom ke menu konteks di 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

Note: Di dalam kode, silahkan ganti semua “Makro Saya” dengan nama Makro yang telah Anda buat di Excel.

3. tekan lain + Q kunci untuk keluar dari Microsoft Visual Basic untuk Aplikasi jendela.

Sekarang Anda kembali ke antarmuka Excel, setelah mengklik kanan pada sel, Anda akan melihat tombol baru dengan nama yang ditentukan ditambahkan ke menu klik kanan seperti gambar di bawah ini. Klik tombol tersebut akan segera memicu Makro yang sesuai.


Terkait artikel:

Alat Produktivitas Kantor Terbaik

🤖 Kutools AI Ajudan: Merevolusi analisis data berdasarkan: Eksekusi Cerdas   |  Hasilkan Kode  |  Buat Rumus Khusus  |  Analisis Data dan Hasilkan Grafik  |  Aktifkan Fungsi Kutools...
Fitur Populer: Temukan, Sorot, atau Identifikasi Duplikat   |  Hapus Baris Kosong   |  Gabungkan Kolom atau Sel tanpa Kehilangan Data   |   Putaran tanpa Formula ...
Pencarian Super: VLookup Beberapa Kriteria    VLookup Nilai Berganda  |   VLookup di Beberapa Lembar   |   Pencarian Fuzzy ....
Daftar Drop-down Lanjutan: Buat Daftar Drop Down dengan Cepat   |  Daftar Drop Down yang Bergantung   |  Multi-pilih Drop Down List ....
Manajer Kolom: Tambahkan Jumlah Kolom Tertentu  |  Pindahkan Kolom  |  Alihkan Status Visibilitas Kolom Tersembunyi  |  Bandingkan Rentang & Kolom ...
Fitur Unggulan: Fokus Kisi   |  Tampilan Desain   |   Bar Formula Besar    Manajer Buku Kerja & Lembar   |  Perpustakaan Sumberdaya (Teks otomatis)   |  Pemetik tanggal   |  Gabungkan Lembar Kerja   |  Enkripsi/Dekripsi Sel    Kirim Email berdasarkan Daftar   |  Filter Super   |   Filter Khusus (filter tebal/miring/coret...) ...
15 Perangkat Teratas12 Teks Tools (Tambahkan Teks, Hapus Karakter, ...)   |   50 + Grafik jenis (Gantt Chart, ...)   |   40+ Praktis Rumus (Hitung usia berdasarkan ulang tahun, ...)   |   19 Insersi Tools (Masukkan Kode QR, Sisipkan Gambar dari Jalur, ...)   |   12 Konversi Tools (Angka ke Kata, Konversi Mata Uang, ...)   |   7 Gabungkan & Pisahkan Tools (Lanjutan Gabungkan Baris, Pisahkan Sel, ...)   |   ... dan banyak lagi

Tingkatkan Keterampilan Excel Anda dengan Kutools for Excel, dan Rasakan Efisiensi yang Belum Pernah Ada Sebelumnya. Kutools for Excel Menawarkan Lebih dari 300 Fitur Lanjutan untuk Meningkatkan Produktivitas dan Menghemat Waktu.  Klik Di Sini untuk Mendapatkan Fitur yang Paling Anda Butuhkan...

Deskripsi Produk


Tab Office Membawa antarmuka Tab ke Office, dan Membuat Pekerjaan Anda Jauh Lebih Mudah

  • Aktifkan pengeditan dan pembacaan tab di Word, Excel, PowerPoint, Publisher, Access, Visio, dan Project.
  • Buka dan buat banyak dokumen di tab baru di jendela yang sama, bukan di jendela baru.
  • Meningkatkan produktivitas Anda sebesar 50%, dan mengurangi ratusan klik mouse untuk Anda setiap hari!
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