Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Keep form settings with other screen resolution?

0

Hello,

i was wondering if there's a code in vba

to keep the whole userform settings of the controls without changing them

when use on a other computer with other screen resolutions

i have noticed that everything changed when the resolutions is larger then the default screen where the userforms has been created.

thank you

Answer
Discuss

Discussion

Check the update in my answer. You will have to do some testing to get things dialed-in but I think you're on the right track!
don (rep: 1989) Sep 8, '20 at 3:40 am
Add to Discussion

Answers

0
Selected Answer

I think that your issue might be related to zoom level that is on high resolution screens, which messes with proportions for some programs.

If this is the case, you could set things according to the size of excel using Application.Width and Application.Height, or you could use a more complex setup to dynamically size it that might work that I found here.

In case that link stops working, the code from it is here:

Option Explicit
'Function to get screen resolution
#If VBA7 Then
    Private Declare PtrSafe Function GetSystemMetrics32 Lib "user32" Alias "GetSystemMetrics" (ByVal nIndex As LongPtr) As Long

    'Functions to get DPI
    Private Declare PtrSafe Function GetDC Lib "user32" (ByVal hwnd As LongPtr) As Long
    Private Declare PtrSafe Function GetDeviceCaps Lib "gdi32" (ByVal hDC As LongPtr, ByVal nIndex As LongPtr) As Long
    Private Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hwnd As LongPtr, ByVal hDC As LongPtr) As Long
#Else
    Private Declare Function GetSystemMetrics32 Lib "user32" Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
    'Functions to get DPI
    Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
    Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
#End If
Private Const LOGPIXELSX = 88  'Pixels/inch in X
Private Const POINTS_PER_INCH As Long = 72 'A point is defined as 1/72 inches

'Return DPI
Public Function PointsPerPixel() As Double
 Dim hDC As Long
 Dim lDotsPerInch As Long

 hDC = GetDC(0)
 lDotsPerInch = GetDeviceCaps(hDC, LOGPIXELSX)
 PointsPerPixel = POINTS_PER_INCH / lDotsPerInch
 ReleaseDC 0, hDC
End Function

Private Sub UserForm_Initialize()
Dim add_file_btn As CommandButton
Dim w As Long, h As Long
    w = GetSystemMetrics32(0) ' Screen Resolution width in points
    h = GetSystemMetrics32(1) ' Screen Resolution height in points
With Me
    .StartUpPosition = 2
    .Width = w * PointsPerPixel * 0.5 'Userform width= Width in Resolution * DPI * 50%
    .Height = h * PointsPerPixel * 0.5 'Userform height= Height in Resolution * DPI * 50%
End With
End Sub

The code was posted by Lo Bellin. I have no idea if its his code or not, but giving him credit since he posted it.

Update

You are making a uesrform with a lot of controls inside of it and you will also have to change the size of the controls as it seems that you cannot have them auto-fit for changing screen sizes. Based on your screen shots, this seems to be the issue.

Ron de Bruin is an amazing guy who has a page on resizing userforms and controls that should help you.

That page should have what you need and the relevant code is this part:

With Me

    'Change controls/font on the userform
    For Each ControlOnForm In .Controls
        With ControlOnForm
            .Top = .Top * SizeCoefForMac
            .Left = .Left * SizeCoefForMac
            .Width = .Width * SizeCoefForMac
            .Height = .Height * SizeCoefForMac
            On Error Resume Next
            .Font.Size = .Font.Size * SizeCoefForMac
            On Error GoTo 0
        End With
    Next

End With

This shows you how to loop through the controls and change their size. You would replace .Width *SizeCoefForMac to something like this from the code that you posted: w * PointsPerPixel * 0.82

This should help getting the controls now "auto-resized" for the userform.

Discuss

Discussion

Hello,

Thank you for this sample of code
it seems working fine on windows 8.1
but a little changes of the 0.5
i used this on windows 8.1
With Me
    .StartUpPosition = 2
    .Width = w * PointsPerPixel * 0.82  'Userform width= Width in Resolution * DPI * 50%
    .Height = h * PointsPerPixel * 0.95 'Userform height= Height in Resolution * DPI * 50%
End With


but on windows 10
it still mess up the widht and height
any ideas please.
GhostofWanted (rep: 46) Sep 7, '20 at 12:25 pm
Update your question with the code that you tried or just upload a sample file and it will be much easier. I have Windows 10 so if you tell me what to look for I can then see if its just a quick fix or what.
don (rep: 1989) Sep 7, '20 at 1:30 pm
Hello,

i have uploaded the sample file
and added also a sheet called Screenshot
the first image is win10 second win8.1
as you will see that win10 looks different then win8.1

Thank you
GhostofWanted (rep: 46) Sep 7, '20 at 10:29 pm
Thanks don

you're amazing :)
GhostofWanted (rep: 46) Sep 9, '20 at 1:12 pm
You're welcome!! I'm glad it worked :)
don (rep: 1989) Sep 12, '20 at 3:27 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login