Self Adjusting Userform Size

心不动则不痛 提交于 2019-12-04 14:44:26

问题


I have been googling for the past several hours and I have found all kinds of neat ways to adjust a Userform to fit the size of different monitors or to add a dragbar in the corner to be able to adjust the size at will manually.

Sadly however this is not what I had in mind. I am unsure if what I want is even doable but if anyone can this community can do it.

The goal for me is have a Userform with lots of information on it to auto adjust in size to show all of the visible controls but no extra dead space. There would be 3 Combo boxes always visible at the top but below those are 26 labels and their associated 5 option button/checkboxes. These 26 rows will all start hidden and only be visible under certain conditions.

The first of the 3 Combo boxes will state how many of the 26 rows MIGHT need to be visible. They however will only be visible if the second Combo box says yes.

I am using Excel 2013. If you need any more information please let me know.

Private Sub ComboBox1_Change()

If Me.ComboBox1.Value > 0 And Me.ComboBox2.Value = "Yes" Then
    Vision
End If

End Sub
Private Sub ComboBox2_Change()

If Me.ComboBox1.Value > 0 And Me.ComboBox2.Value = "Yes" Then
    Vision
End If

End Sub

Private Sub UserForm_Initialize()

With ComboBox1
    .AddItem "1"
    .AddItem "2"
    .AddItem "3"
    .AddItem "4"
    .AddItem "5"
    .AddItem "6"
End With

With ComboBox2
    .AddItem "Yes"
    .AddItem "NO"
End With

With ComboBox3
    .AddItem "1"
    .AddItem "2"
End With

With Me
    .Controls("Label1").Visible = False
    .Controls("Label2").Visible = False
    .Controls("Label3").Visible = False
    .Controls("Label4").Visible = False
    .Controls("Label5").Visible = False
    .Controls("Label6").Visible = False
End With
With Me
    .Controls("Checkbox1").Visible = False
    .Controls("Checkbox2").Visible = False
    .Controls("Checkbox3").Visible = False
    .Controls("Checkbox4").Visible = False
    .Controls("Checkbox5").Visible = False
    .Controls("Checkbox6").Visible = False
End With


End Sub


Private Sub Vision()
Dim n As Long

With Me
    .Controls("Label1").Visible = False
    .Controls("Label2").Visible = False
    .Controls("Label3").Visible = False
    .Controls("Label4").Visible = False
    .Controls("Label5").Visible = False
    .Controls("Label6").Visible = False
End With
With Me
    .Controls("Checkbox1").Visible = False
    .Controls("Checkbox2").Visible = False
    .Controls("Checkbox3").Visible = False
    .Controls("Checkbox4").Visible = False
    .Controls("Checkbox5").Visible = False
    .Controls("Checkbox6").Visible = False
End With

For n = 1 To ComboBox1.Value
    With Me
        .Controls("Label" & n).Visible = True
        .Controls("Checkbox" & n).Visible = True
    End With
Next n

End Sub

回答1:


Here's one possible approach.

Private Sub UserForm_Activate()
    CheckSize
End Sub

Private Sub CommandButton1_Click()
    Me.lblTest.Visible = Not Me.lblTest.Visible
    CheckSize
End Sub

Private Sub CheckSize()
Dim h, w
Dim c As Control

    h = 0: w = 0
    For Each c In Me.Controls
        If c.Visible Then
            If c.Top + c.Height > h Then h = c.Top + c.Height
            If c.Left + c.Width > w Then w = c.Left + c.Width
        End If
    Next c

    If h > 0 And w > 0 Then
        With Me
            .Width = w + 40
            .Height = h + 40
        End With
    End If
End Sub



回答2:


I found that the issue lied with display settings - I have my display set to 100% but others had theirs set to 150% so when they set it back to 100% they could view it correctly



来源:https://stackoverflow.com/questions/28100310/self-adjusting-userform-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!