简体   繁体   English

使用 VBA 格式化 Excel 用户窗体中的多个控件

[英]Formatting Multiple Controls in an Excel UserForm using VBA

I have had a hunt around and have got to a point where I am almost sorted but can't quite complete.我四处打猎,已经到了我几乎被分类但不能完全完成的地步。

I have the following:我有以下内容:

Sub Formats()

    'Form Setup
    '----------------
    With AuditForm
        .BackColor = Grey4
        .BorderColor = Grey1
        .Font.Name = "Arial"
        .ForeColor = Grey1
        .SpecialEffect = 1
    End With
    
    For Each ctrl In AuditForm.Controls
    If TypeName(ctrl) = "TextBox" Then
            ctrl.BackColor = White
            ctrl.BorderColor = Grey1
            ctrl.SpecialEffect = 1
            ctrl.BorderStyle = 1
            ctrl.Font.Name = "Arial"
            
    ElseIf TypeName(ctrl) = "Frame" Then
            ctrl.BackColor = Grey4
            ctrl.BorderColor = Grey1
            ctrl.SpecialEffect = 1
            ctrl.BorderStyle = 1
            ctrl.Font.Name = "Arial"
            ctrl.ForeColor = Grey1
            ctrl.Font.Bold = True
            
    ElseIf TypeName(ctrl) = "Label" Then
            ctrl.BackColor = Grey4
            ctrl.BorderColor = Grey4
            ctrl.SpecialEffect = 1
            ctrl.BorderStyle = 1
            ctrl.Font.Name = "Arial"
            ctrl.ForeColor = Grey1
            ctrl.Font.Bold = True
            
    ElseIf TypeName(ctrl) = "ComboBox" Then
            ctrl.BackColor = White
            ctrl.BorderColor = Grey1
            ctrl.SpecialEffect = 1
            ctrl.BorderStyle = 1
            ctrl.Font.Name = "Arial"
            ctrl.ShowDropButtonWhen = fmShowDropButtonWhenFocus
            ctrl.Style = fmStyleDropDownList
    End If
        
    Next ctrl
        
End Sub

I have each control type I need in the code, but I just want to change the:我在代码中有我需要的每种控件类型,但我只想更改:

For Each ctrl In AuditForm.Controls

To reference any user form that opens.引用打开的任何用户表单。 I have tried For Each ctrl In me.controls but that seems to error out.我试过For Each ctrl In me.controls但这似乎出错了。 The alternative is to copy the block above for each of the userforms in my document, which seems messy.另一种方法是为我的文档中的每个用户窗体复制上面的块,这看起来很乱。

Any help as ever will be appreciated.任何帮助将不胜感激。

Thanks,谢谢,

Bill账单

Bill, just declare your ctrl variable as MSForms.Control比尔,只需将您的 ctrl 变量声明为 MSForms.Control

Dim ctrl As MSForms.Control
For Each ctrl In Me.Controls
   ' it works !
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM