當前位置:編程學習大全網 - 編程軟體 - VBA如何用變量表示控件名?

VBA如何用變量表示控件名?

如果該控件位於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 表示Label2.Caption

Sheet1.OleObjects("CommandButton" & a).Object.Caption 表示CommandButton1.Caption

  • 上一篇:什麽是源代碼,源代碼通過怎樣的方式可以成為軟件?
  • 下一篇:Ftp服務器如何建網站
  • copyright 2024編程學習大全網