小编pee*_*ege的帖子

在项目中更改Evey UserForm的StartUpPosition错误:对象不支持属性

设置: 我有许多带有许多UserForms的项目,当在双监视器系统上打开它们时,默认的StartUpPosition是1 = CenterOwner,它出现在主显示器的右侧(Windows桌面的中心).我正在尝试编写一小段代码,为项目中的每个用户表单更改一个属性.在这种情况下,具体而言,.StartUpPosition = 2(CenterScreen)

使用Microsoft Office Professional Plus 2010

我已经知道如何在Positions.StartUpPosition下使用VBA中的属性编辑器更改StartUpPosition.问题是在这么多项目中有太多的UserForms,我想一次在每个项目上更改它们.最后,我想使用相同的代码通过使用方法的重载参数(FormName,Property,Value)来改变任何属性.现在,我只有一个可以处理StartUpPosition.

当我运行此代码时,打开UserForm时,它按预期工作,覆盖默认的StartUpPosition = 1(CenterOwner),它在屏幕中央显示窗体.

Sub UserForm_Initialize()
    With UserFormName
      .StartUpPosition = 2 'CenterScreen'
    End With
End Sub
Run Code Online (Sandbox Code Playgroud)

但是,当我在这个循环中运行它时,它从模块循环遍历项目的控件,试图一次更改所有表单的默认值,我得到以下错误.

错误: 运行时错误"438":对象不支持此属性或方法.

Sub UserFormStartUp_Center()
    Dim VBComp As Object
    'For each control in project'
    For Each VBComp In Application.VBE.ActiveVBProject.VBComponents
        'Check to see if control is a UserForm'
        If VBComp.Type = 3 Then           '3 = vbext_ct_MSForm'

            'Change Property - StartUpPosition - SAME AS ABOVE'
            With VBComp
                .StartUpPosition = 2 'CenterScreen'
            End With

        End If …
Run Code Online (Sandbox Code Playgroud)

excel vba excel-vba userform

5
推荐指数
1
解决办法
1199
查看次数

标签 统计

excel ×1

excel-vba ×1

userform ×1

vba ×1