1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【ユーザーフォーム】各コントロールをTypeName別に色やフォント設定して統一感のあるデザインにする【ExcelVBA】

Last updated at Posted at 2022-02-04

UFの全コントロールのTypeNameを出力するを実行するとコントロールの型がイミディエイトウインドウに列挙されます。

Private Sub UFの全コントロールのTypeNameを出力する()
  
  Dim e As Control
  For Each e In Me.Controls
    Debug.Print e.Name, TypeName(e)
  Next

End Sub

UFの全コントロールを着色する内のSelect文で仕分けてから着色なりフォント設定なりすると統一感のあるデザインになります。

ユーザーフォーム自体はコントロールではないのでコントロールを入れる引数の型はVariantにしてます。


Enum clrUF
   = 0
  _淡い = 12632256
  薄い橙 = 8696052
End Enum

Private Sub UFの全コントロールを着色する()
  
  Call 指定コントロールを着色(Me, clrUF.黒, clrUF._淡い)
  
  Dim e As Control
  For Each e In Me.Controls
    Call 指定コントロールのフォント設定(e, "Meiryo UI", 12, False)
    
    Select Case TypeName(e)
      Case "Label", "Frame"
        Call 指定コントロールを着色(e, clrUF.黒, clrUF._淡い)
        
      Case "ListView4", "TextBox"
        Call 指定コントロールを着色(e, clrUF.黒, clrUF.薄い橙, clrUF._淡い)
        
      Case "CommandButton"
        Call 指定コントロールを着色(e, clrUF._淡い, clrUF.黒)
    End Select
  Next

End Sub

Private Sub 指定コントロールのフォント設定(myControl As Variant, _
                                          フォント As String, _
                                            サイズ As Double, _
                                    Optional Bold As Boolean)
  Rem myControlがVariantなのはMeがControlではないため
  
  With myControl.Font
    .Name = フォント
    .Size = サイズ
    .Bold = Bold
  End With
  
End Sub

Private Sub 指定コントロールを着色(myControl As Variant, _
                                     背景色, _
                                     文字色, _
                            Optional 境界色)
  Rem myControlがVariantなのはMeがControlではないため
  
  On Error Resume Next
  With myControl
    .BackColor = 背景色
    .ForeColor = 文字色
    If IsMissing(境界色) = False Then: .BorderColor = 境界色
  End With
End Sub

ListViewTypeNameListView4なの初めて知った。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?