你好,
我正在尝试设置一个表单,它是一个日历,用户可以从中选择一个日期(默认情况下显示当前月份)。该表单由 42 个命令按钮组成(我保留了默认名称,即 CommandButton1),我正在设置日期编号。
目前,我为每个按钮编写了一段冗长的代码(我使用 Excel 来生成它而不是全部输入),如果按钮在相关月份之外,它会锁定和隐藏按钮,如下所示:
NewDate.CommandButton1.Caption = Format(DATlngFirstMonth - DATintDayNumFirst + DATintX, "dd")
If DATintX < DATintDayNumFirst Then
With NewDate.CommandButton1
.Locked = True
.Visible = DATbooShowExtraDays
.ForeColor = RGB(150, 150, 150)
End With
Else
With NewDate.CommandButton1
.Locked = False
.Visible = True
.ForeColor = RGB(0, 0, 0)
End With
End If
我知道我可以通过以下方式引用命令按钮:
Dim objCommandButton As Object
Set objCommandButton = NewDate.CommandButton1
..这稍微整理了代码。但是我想做的是将命令按钮称为字符串,这样我就可以遍历所有 42 个,即。
Dim n as integer
n = 1
Do Until n > 42
Set objCommandButton = NewDate.CommandButton & n
'Some operations
n = n + 1
Loop
非常感谢您的帮助。
您可以遍历表单的所有控件。尝试
Sub LoopButtons()
Dim it As Object
For Each it In NewDate.Controls
Debug.Print it.Name
Next it
End Sub
然后你可以用条件表达式 (if ... then) 代替 Debug.Print 或其他什么。例如
If Instr(it.Name, "CommandButton") Then
'do your code
end if
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句