Excel VBA中如何初始化控件属性?

Excel VBA中如何初始化控件属性?可在运行时用宏中的Excel VBA代码初始化控件。例如,可以向列表框填充内容、设置文本值或设置选项按钮。 Excel VBA How to initialize control properties? Macro can be used at run-time initialization code in Excel VBA controls. For example, to fill a list box, set the text value or set the option button.下例使用 AddItem 方法向列表框添加数据,然后设置文本框的值并显示窗体。The following example uses the AddItem method to add data to list box, and then set the value of the text box and display the form.Private Sub GetUserName() With UserForm1 .lstRegions.AddItem “North” .lstRegions.AddItem “South” .lstRegions.AddItem “East” .lstRegions.AddItem “West” .txtSalesPersonID.Text = “00000” .Show ‘ … End WithEnd Sub 还可以在窗体的 Initialize 事件中用代码设置窗体上控件的初始值。在 Initialize 事件中设置控件的初始值的好处在于:初始化代码与窗体存储在一起。这样,如果把该窗体复制到其他项目中,那么运行 Show 方法显示对话框时,其中的控件将被初始化。 Can also be used in the Initialize event of the form controls on the form code to set the initial value. Control in the Initialize event set the initial value of the advantage: the initialization code and stored with the form. Thus, if the form is copied to other projects, then run the Show method to display the dialog box, which controls will be initialized.Private Sub UserForm_Initialize() UserForm1.lstNames.AddItem “Test One” UserForm1.lstNames.AddItem “Test Two” UserForm1.txtUserName.Text = “Default Name”End Sub
贪婪是最真实的贫穷,满足是最真实的财富

Excel VBA中如何初始化控件属性?

相关文章:

你感兴趣的文章:

标签云: