Maximising UserForm view

AndyEd

Board Regular
Joined
May 13, 2020
Messages
124
Office Version
  1. 365
Platform
  1. Windows
I am using the following code to maximise a UserForm, however it isn't aligned quite right and I can only just see a scroll bar at the bottom. Does anyone know a reason for this?

Thanks,

VBA Code:
    With Application
        Me.Top = .Top
        Me.Left = .Left
        Me.Height = .Height
        Me.Width = .Width
    End With
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Maybe....
Code:
Me.Top = 1
Me.Left = 1
Me.Height = Application.UsableHeight
Me.Width = Application.UsableWidth
HTH. Dave
 
Upvote 0
You are probably refering to when the application window is not maximized.

Try this alternative and see what happens :
VBA Code:
Option Explicit

Private Type RECT
    Left As Long
    Top  As Long
    Right As Long
    Bottom As Long
End Type

#If VBA7 Then
    Private Declare PtrSafe Function IUnknown_GetWindow Lib "shlwapi" Alias "#172" (ByVal pIUnk As IUnknown, ByVal hUF As LongPtr) As Long
    Private Declare PtrSafe Function SetWindowPos Lib "user32" (ByVal hwnd As LongPtr, ByVal hWndInsertAfter As LongPtr, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    Private Declare PtrSafe Function GetWindowRect Lib "user32" (ByVal hwnd As LongPtr, lpRect As RECT) As Long
    Private Declare PtrSafe Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
#Else
    Private Declare Function IUnknown_GetWindow Lib "shlwapi" Alias "#172" (ByVal pIUnk As IUnknown, ByVal hUF As Long) As Long
    Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
#End If

Private Sub UserForm_Click()

    Const SM_CXFRAME = 32&, SM_CYFRAME = 33&
    Dim tRect As RECT
    #If Win64 Then
        Const NULL_PTR = 0^
        Dim hwnd As LongLong
    #Else
        Const NULL_PTR = 0&
        Dim hwnd As Long
    #End If

    Call IUnknown_GetWindow(Me, VarPtr(hwnd))
    Call GetWindowRect(Application.hwnd, tRect)
    With tRect
        .Left = .Left - 2& * GetSystemMetrics(SM_CYFRAME)
        .Right = (.Right - .Left) + 2& * GetSystemMetrics(SM_CYFRAME)
        .Bottom = (.Bottom - .Top) + 2& * GetSystemMetrics(SM_CXFRAME)
        Call SetWindowPos(hwnd, NULL_PTR, .Left, .Top, .Right, .Bottom, ByVal 0&)
    End With

End Sub
 
Upvote 0
Hi Jaafar

I've used the above and all it does it create a smaller userform window. It does not extend to the size of the monitor used.
 
Upvote 0

Forum statistics

Threads
1,215,084
Messages
6,123,021
Members
449,092
Latest member
ikke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top