Skip to content

Instantly share code, notes, and snippets.

@YujiFukami
Created June 16, 2023 09:01
Show Gist options
  • Save YujiFukami/8c9f48d9d1232a7815752d2c502ac63a to your computer and use it in GitHub Desktop.
Save YujiFukami/8c9f48d9d1232a7815752d2c502ac63a to your computer and use it in GitHub Desktop.
Public Sub MakeCommandBtns(ByRef TargetForm As UserForm, _
ByRef RowCount As Long, _
ByRef ColCount As Long, _
ByRef Height As Double, _
ByRef Width As Double, _
Optional ByRef HeadStr As String = "Btn", _
Optional ByRef StartTop As Double = 10, _
Optional ByRef StartLeft As Double = 10, _
Optional ByRef GapHeight As Double = 3, _
Optional ByRef GapWidth As Double = 3)
'ユーザーフォームにコマンドボタンを設置する。
'https://docs.microsoft.com/ja-jp/office/vba/language/reference/user-interface-help/add-method-microsoft-forms
'20211126
'http://blog.livedoor.jp/aero_iki-jibundakemacro/archives/35100673.html
'引数
'TargetForm ・・・対象のユーザーフォーム
'RowCount ・・・ボタンの縦方向個数
'ColCount ・・・ボタンの横方向個数
'Height ・・・ボタン1個の高さ
'Width ・・・ボタン1個の幅
'[HeadStr] ・・・ボタン名の頭文字(省略なら"Btn")
'[StartTop] ・・・ボタン設置の開始高さ位置(省略なら10)
'[StartLeft]・・・ボタン設置の開始横位置(省略なら10)
'[GapHeight]・・・ボタンの間のギャップ(間隔)の高さ(省略なら3)
'[GapWidth] ・・・ボタンの間のギャップ(間隔)の幅(省略なら3)
Dim I As Long
Dim J As Long
Dim TmpBtn As CommandButton '追加するコマンドボタン
For I = 1 To RowCount
For J = 1 To ColCount
Set TmpBtn = TargetForm.Controls.Add("Forms.CommandButton.1") 'コマンドボタンを追加
With TmpBtn
.Name = HeadStr & I & "_" & J 'コマンドボタンの名前設定 [頭文字][縦番号]_[横番号]
.Top = StartTop + Height * (I - 1) + GapHeight * (I - 1) 'コマンドボタンの縦位置設定
.Left = StartLeft + Width * (J - 1) + GapWidth * (J - 1) 'コマンドボタンの横位置設定
.Height = Height 'コマンドボタンの高さ設定
.Width = Width 'コマンドボタンの幅設定
End With
Next J
Next I
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment