Excel 2016 VBA: Adjust userform size to match size of the visible frame

jedwardo

Board Regular
Joined
Aug 21, 2012
Messages
122
I have a userform keyboard named (UFKeyboard). In this form there are 3 frames, Frame1, Frame2, and Frame3, each containing a separate keyboard. When one keyboard is called the other 2 frames are hidden. All 3 keyboards are different sizes though so what I'm trying to do is find a way to adjust the size of the userform to match the size of the visible keyboard. That way when the numberpad keyboard shows there's not a large grey box covering most of the screen. Not to sure where to start searching for this one. Here's my current initialize:

Code:
Private Sub UserForm_Initialize()
    Dim strBox As String
    Dim i As Integer
    Dim varFrame As Variant, varLabel As Variant
    Dim clsBtnKey As clsBtnKeys
    Dim ctlLoop As MSForms.Control
    Set colKeyboardKeys = New Collection
    
        For i = 1 To 3
            varFrame = Me("Frame" & i)
                Select Case i
                    Case Is = 1, Is = 2, Is = 3
        
                        For Each ctlLoop In Me("Frame" & i).Controls
                            If TypeOf ctlLoop Is MSForms.commandbutton Then
                                Set clsBtnKey = New clsBtnKeys
                                Set clsBtnKey.Control = ctlLoop
                                colKeyboardKeys.Add clsBtnKey
                            End If
                        Next ctlLoop
        
                        For Each ctlLoop In Me("Frame" & i).Controls
                            If TypeOf ctlLoop Is MSForms.commandbutton Then
                                ctlLoop.Caption = Left(ctlLoop.Tag, 1)
                            End If
                        Next ctlLoop
                        cbnBackSpace.Caption = "Backspace"
                        cbnEnter.Caption = "Enter"
                        cbnBackspaceB.Caption = "Backspace"
                        cbnEnterB.Caption = "Enter"
                        cbnBackSpaceC.Caption = "Backspace"
                        cbnEnterC.Caption = "Enter"
                        TextBox1.SetFocus
                    Case Else: Exit For
                End Select
            Next
    
' Fetch label caption for textbox2 on UFKeyboard

'    strBox = Mid(UserFormContacts.ActiveControl.Name, 8)
'    varLabel = "Label" & strBox
'    UFKeyboard.TextBox2.Value = UserFormContacts(varLabel).Caption
        
End Sub

Thanks,
Jordan
 
Last edited:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
This changes the size of the userform to the size of the frame. It may work. Dave
Code:
UserForm1.Frame1.Parent.Top = UserForm1.Frame1.Top
UserForm1.Frame1.Parent.Left = UserForm1.Frame1.Left
UserForm1.Frame1.Parent.Width = UserForm1.Frame1.Width
UserForm1.Frame1.Parent.Height = UserForm1.Frame1.Height
 
Upvote 0
You are welcome. Oddly enough, I was playing with frames just yesterday. That code was one of my errors. Thanks for posting your outcome. Dave
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

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