如果该控件位于UserForm窗体中,那么我们可以用Controls集合来表示指定的控件,
例如,UserForm1中有三个控件,Label1,Label2,TextBox1
那么,我们可以这样表示:
a = 1 : b = 2
UserForm1.Controls("Label" & a).Caption 表示Label1.Caption
UserForm1.Controls("Label" & b).Caption 表示Label2.Caption
UserForm1.Controls("TextBox" & a).Text 表示TextBox1.Text
如果你的控件位于工作表中,那么我们可以使用OleObjects集合来表示指定的控件,
例如,Sheet1工作表中有三个控件,Label1,Label2,CommandButton1
a = 1 : b = 2
那么我们可以这样表示,注意Object不可省略:
Sheet1.OleObjects("Label" & a).Object.Caption 表示Label1.Caption
Sheet1.OleObjects("Label" & b).Object.Caption 表示Label1.Caption
Sheet1.OleObjects("CommandButton" & a).Object.Caption 表示CommandButton1.Caption
vba 设置变量替代表后无法引用控件
最新推荐文章于 2021-12-08 18:00:52 发布