Join Forum BE-ORG Butuh bantuan tentang Excel?? Gabung aja di Forum BE-ORG

Cara Menonaktifkan Tombol Close (X) UserForm Excel

Untuk tujuan tertentu kadang pengguna Excel ingin menonaktifkan atau mendisable button Close (X) pada UserForm Excel, dengan begitu tombol Close (X) ini tidak akan lagi bisa di klik, sehingga UserForm ini tidak bisa di tutup melalui tombol Close (X) ini, Sebagai gantinya biasanya digunakan tombol Command_Button sebagai gantinya.

Agar lebih jelas, berikut perbedaaan tampilan UserForm normal dengan tombol Close(X) dan Tombol Close(X) yang di disable atau yg dinonaktifkan.

Disable Close Button UserForm

Untuk menonaktifkan tombol Close (X) pada UserForm Excel caranya pun sangat mudah, yakni dengan menambahkan fungsi API (Application Programming Interface) yang diletakkan di Module.

[alert-success]Pengertian API (Application Programming Interface) adalah sekumpulan perintah, fungsi dan protocol yang dapat digunakan oleh programer saat membangun perangkat lunak untuk system operasi tertentu.[/alert-success]

Berikut langkah-langkah mendisable tombol Close (X) di UserForm Excel:

  1. Buka aplikasi Microsoft Excel dan tekan Alt+F11 untuk menampilkan aplikasi Visual Basic For Application.
  2. Tambahkan sebuah UserForm dan Module.
  3. Masukkan script VBA berikut ini ke dalam Module1.
  4. Option Explicit
    Private Const MF_BYPOSITION = &H400
    Private Const MF_REMOVE = &H1000
    Private Const WS_SYSMENU = &H80000
    Private Const GWL_STYLE = (-16)
    Private hwnd As Long
    Private lStyle As Long
    
    #If VBA7 And Win64 Then
    Private Declare PtrSafe Function FindWindow Lib "user32" _
            Alias "FindWindowA" (ByVal lpClassName As String, _
            ByVal lpWindowName As String) As LongLong
    Private Declare PtrSafe Function GetWindowLong Lib "user32" _
            Alias "GetWindowLongA" (ByVal hwnd As Long, _
            ByVal nIndex As Long) As LongLong
    Private Declare PtrSafe Function SetWindowLong Lib "user32" _
            Alias "SetWindowLongA" (ByVal hwnd As Long, _
            ByVal nIndex As Long, _
            ByVal dwNewLong As Long) As LongLong
    Private Declare PtrSafe Function DrawMenuBar Lib "user32" _
            (ByVal hwnd As Long) As LongLong
    Private Declare PtrSafe Function GetMenuItemCount Lib "user32" _
                    (ByVal hMenu As Long) As LongLong
    Private Declare PtrSafe Function GetSystemMenu Lib "user32" _
                    (ByVal hwnd As Long, _
                    ByVal bRevert As Long) As LongLong
    Private Declare PtrSafe Function RemoveMenu Lib "user32" _
                    (ByVal hMenu As Long, ByVal nPosition As Long, _
                    ByVal wFlags As Long) As LongLong
    #Else
    Private Declare Function FindWindow Lib "user32" _
                    Alias "FindWindowA" _
                    (ByVal lpClassName As String, _
                    ByVal lpWindowName As String) As Long
    Private Declare Function GetWindowLong Lib "user32" _
                    Alias "GetWindowLongA" _
                    (ByVal hwnd As Long, _
                    ByVal nIndex As Long) As Long
    Private Declare Function SetWindowLong Lib "user32" _
                    Alias "SetWindowLongA" _
                    (ByVal hwnd As Long, _
                    ByVal nIndex As Long, _
                    ByVal dwNewLong As Long) As Long
    Private Declare Function DrawMenuBar Lib "user32" _
                    (ByVal hwnd As Long) As Long
    Private Declare Function GetMenuItemCount Lib "user32" _
                    (ByVal hMenu As Long) As Long
    Private Declare Function GetSystemMenu Lib "user32" _
                    (ByVal hwnd As Long, ByVal bRevert As Long) As Long
    Private Declare Function RemoveMenu Lib "user32" _
                    (ByVal hMenu As Long, ByVal nPosition As Long, _
                    ByVal wFlags As Long) As Long
    #End If
    
    Sub DisableXCloseButton(oForm As Object)
    Dim hMenu As Long
    Dim menuItemCount As Long
    hwnd = FindWindow("ThunderDFrame", oForm.Caption)
    hMenu = GetSystemMenu(hwnd, 0)
    If hMenu Then
      menuItemCount = GetMenuItemCount(hMenu)
      Call RemoveMenu(hMenu, menuItemCount - 1, MF_REMOVE Or MF_BYPOSITION)
      Call RemoveMenu(hMenu, menuItemCount - 2, MF_REMOVE Or MF_BYPOSITION)
      Call DrawMenuBar(hwnd)
    End If
    End Sub
    
  5. Selanjutnya tambahkan script VBA berikut di UserForm1.
  6. Private Sub UserForm_Initialize()
    Module1.DisableXCloseButton Me
    End Sub
    
  7. Selesai, Tekan tombol F5 untuk menjalankan UserForm dan jika semua langkah yang di ikuti sudah benar, maka pada tombol Close (X) UserForm pasti sudah terdisable.

Untuk contoh file yang sudah jadi bisa di download di halaman berikutnya 😀

One Comment

Add a Comment

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

Situs ini menggunakan Akismet untuk mengurangi spam. Pelajari bagaimana data komentar Anda diproses.