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

Cara Menyembunyikan Tombol Close (X) UserForm Excel

Pada Artikel sebelumnya kita sudah belajar cara menonaktifkan tombol Close (X) UserForm Excel, selain di nonaktifkan tombol Close (X) UserForm juga bisa disembunyikan atau di hilangkan.

Untuk lebih jelasnya bisa di lihat pada gambar di bawah ini:

Hide Close Button UserForm

Berikut langkah-langkah menyembunyikan 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.HideXCloseButton Me
    End Sub
    
  7. Selesai, Tekan tombol F5 untuk menjalankan UserForm dan jika semua langkah yang di ikuti sudah benar, maka seharusnya tombol Close (X) UserForm sudah hilang.

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

Add a Comment

Your email address will not be published. Required fields are marked *