自我调整用户形态大小

时间:2015-01-22 23:06:26

标签: excel-vba resize excel-2013 userform vba

过去几个小时我一直在谷歌搜索,我发现各种简洁的方法可以调整Userform以适应不同显示器的大小,或者在角落添加拖动条以便能够手动调整大小

然而,遗憾的是,这不是我的想法。我不确定我想要的是否是可行的,但如果有人可以这个社区可以做到。

我的目标是拥有一个包含大量信息的Userform,可以自动调整大小以显示所有可见控件但不会有额外的死区。顶部总会有3个组合框可见,但下面是26个标签及其相关的5个选项按钮/复选框。这26行将全部隐藏起来,只在某些条件下可见。

3个组合框中的第一个将说明可能需要看到的26行中有多少行。但是,只有当第二个组合框显示“是”时,它们才会显示。

我正在使用Excel 2013.如果您需要更多信息,请告诉我。

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

2 个答案:

答案 0 :(得分:2)

这是一种可能的方法。

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

答案 1 :(得分:-1)

我发现该问题与显示设置有关-我将显示设置为100%,但其他人将其显示设置为150%,因此当他们将其设置回100%时,他们可以正确查看