vba - 使用 VBA 调整用户窗体及其控件的大小

标签 vba excel userform

我正在尝试使用 VBA 调整用户窗体及其控件的大小,以适应不同大小的监视器。以下是我正在使用的代码,它基于 Ron DeBruin 的代码 (http://www.rondebruin.nl/mac/mac022.htm)。

本质上,代码旨在缩放用户窗体的大小和位置及其所有控件。

问题是我在执行时遇到错误(如下所示)

"Run-time error '-2147467259(80004005)': Method 'Properties' of object '_VBComponent' failed"

我尝试替换 .Properties("Top").Top我得到了Object doesn't support this property or method错误。

DeBruin 先生的代码从此开始;但我不知道为什么它不起作用。任何帮助肯定会受到赞赏。
Sub ChangeUserFormAndControlsSize()
    Dim AppUserform As Object
    Dim FormControl As Object
    Dim NameUserform As String
    Dim SizeCoefficient As Single

    SizeCoefficient = wsControls.Range("SizeCoefficient")

    NameUserform = "form_APScheduler"

    Set AppUserform = ThisWorkbook.VBProject.VBComponents(NameUserform)
    With AppUserform
        .Properties("Top") = .Properties("Top") * SizeCoefficient   '*** ERROR OCCURS HERE
        .Properties("Left") = .Properties("Left") * SizeCoefficient
        .Properties("Height") = .Properties("Height") * SizeCoefficient
        .Properties("Width") = .Properties("Width") * SizeCoefficient
    End With

    For Each FormControl In AppUserform.Designer.Controls
        With FormControl
            .Top = .Top * SizeCoefficient
            .Left = .Left * SizeCoefficient
            .Width = .Width * SizeCoefficient
            .Height = .Height * SizeCoefficient

            On Error Resume Next
            .Font.Size = .Font.Size * SizeCoefficient
            On Error GoTo 0
        End With
    Next FormControl

End Sub

最佳答案

根据您的最后评论,这里是一些示例代码,展示了如何在运行时更改属性,而无需访问 VBIDE.VBProject 对象。当然,这些变化不会持续。

Option Explicit
Sub testForm()
Dim UF As form_APScheduler
Dim FormControl As MSForms.Control
Dim SizeCoefficient As Double

    SizeCoefficient = inputNumber("Scale Factor: ", "Form", 1)
    Set UF = New form_APScheduler
    With UF
        .Top = .Top * SizeCoefficient
        .Left = .Left * SizeCoefficient
        .Width = .Width * SizeCoefficient
        .Height = .Height * SizeCoefficient
    End With
    For Each FormControl In UF.Controls
        With FormControl
            .Top = .Top * SizeCoefficient
            .Left = .Left * SizeCoefficient
            .Width = .Width * SizeCoefficient
            .Height = .Height * SizeCoefficient

            On Error Resume Next
            .Font.Size = .Font.Size * SizeCoefficient
            On Error GoTo 0
        End With
    Next FormControl
    UF.Show
    Unload UF
End Sub
Function inputNumber(prompt As String, title As String, defValue As Variant) As Variant
    inputNumber = Application.InputBox(prompt, title, defValue, , , , , 1)
End Function

关于vba - 使用 VBA 调整用户窗体及其控件的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24668368/

相关文章:

Excel 2013 模态 VBA 用户窗体在用户窗体可见时更改 Activeworkbook

excel - 使用循环验证用户窗体以检查空控件

excel - 改进选择案例

javascript - 使用 VBA 单击网页按钮

Excel VBA(非常慢)如果满足条件则将整行移动到底部

sql - 当用户的 SQL Server 密码过期时,从 VBA 执行 sp_password(存储过程)

java - 使用java编写大型excel文件的API

Java 8 和文件处理 30 MB excel

vba - 让 MS Office 用户窗体检测调用它的子例程

vba - 当用户窗体启动时,如何在标签中显示当前打印机?