vba控件常规使用

上传人:ba****u6 文档编号:174117333 上传时间:2022-12-14 格式:DOCX 页数:29 大小:62.31KB
返回 下载 相关 举报
vba控件常规使用_第1页
第1页 / 共29页
vba控件常规使用_第2页
第2页 / 共29页
vba控件常规使用_第3页
第3页 / 共29页
点击查看更多>>
资源描述
vba控件常规使用UserForm 基础如何显示UserForm以下是用于显示UserForm编程语法是:UserFormName .Show要显示名为UserForml, UserForm使用以下代码:UserForml.Show不显示它实际上还能加载UserForm装入内存。复杂UserForm可能需要几秒钟以显示。 因为预先您能加载到内存,UserForm可决定何时导致此开销。要加载到内存UserForml 不显示它,使用以下代码:Load UserForml若要显示UserForm,必须使用以前已显示显示方法。如何以暂时隐藏UserForm如果要暂时隐藏UserForm,使用隐藏方法。可能想要隐藏UserForm如果应用程序涉 及用户窗体之间移动。要隐藏UserForm,使用以下代码:UserForml.Hide如何从内存删除UserForm要从内存,删除UserForm使用Unioad语句。要卸载,名为UserForml, UserForm使 用以下代码:Unload UserForml如果您卸载UserForm,是与UserForm或者,是与UserForm上控件的事件过程中 (例如,您单击CommandButton控件),您可以使用我关键字代替的UserForm 名称。将关键字用于卸载UserForm, Me 使用以下代码:Unload Me如何使用UserForm事件支持许多预定义事件,可以附加到VBA过程。在事件发生时,该附加到事件过程运行。 单个操作由用户执行可初始化多事件。之间最经常对UserForm使用事件是Initialize事 件、Click事件,和Terminate事件。注意包含事件过程Visual Basic模块可能称为后面UserForm模块。模块包含事件 过程是不可见的 VisualBasic 编辑器 Project MicrosoftInternetExplorer 窗口 Modules 集 合中。您必须双击正文部分UserForm以查看UserForm代码模块。如何捕获UserForm事件要捕获UserForm事件,请按照下列步骤操作:1. Excel中创建新工作簿。2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。4. 双击以显示代码窗口对于 UserForm UserForm 。 模块, 中键入如下代码:Private Sub UserForm_Click()Me.Height = Int(Rnd * 500) Me.Width = Int(Rnd * 750) End SubPrivate Sub UserForm_Initialize() Me.Caption = Events Events Events! Me.BackColor = RGB(10, 25, 100)End Sub Private Sub UserForm_Resize() msg = Width: & Me.Width & Chr(10) & Height: &5. Me. HeightMsgBox prompt:=msg, Title:=Resize Event End SubPrivate Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)msg = Now Unloading & Me.Caption MsgBox prompt:=msg, Title:=QueryClose Event End SubPrivate Sub UserForm_Terminate() msg = Now Unloading & Me.Caption MsgBox prompt:=msg, Title:=Terminate Event End Sub6. 在运行菜单上,单击运行子过程/用户窗体。UserForm首先加载,时宏使用Initialize事件改为事件事件事件!和BackColor属 性以深蓝色的UserForm Caption属性。当您单击UserForm,您初始化Click事件。调整UserForm Click事件。因为您创建Resize事件,过程单击UserForm后收到两个消息框。因为Click事件代码更改宽度属 性和Height属性是UserForm Resize事件发生两次。关闭UserForm初始化QueryClose事件。QueryClose事件显示消息框包含标题为Initialize事件,您赋予UserForm代码中。可以使用时要执行特定的操作集如果用户关 闭 UserForm QueryClose 事件。然后生成一个消息框,指出标题为UserForm是UserForml Terminate事件。从内存 中删除UserForm并返回到其原始状态标题为UserForm后Terminate事件发生。 如何防止UserForm关闭通过关闭按钮当您运行UserForm,关闭按钮添加到UserForm窗口的右上角。如果要防止UserForm关闭通过关闭按钮,您必须捕获QueryClose事件。QueryClose事件UserForm是从内存中卸载之前发生。使用QueryClose事件 CloseMode CloseMode 参数来确定如何 UserForm 关闭。vbFormControlMenu 值为 CloseMode CloseMode参数表示时,单击关闭按钮。要保持活动,UserForm将Cancel 取消对QueryClose事件参数为True。要使用QueryClose事件来防止UserForm 关闭通过关闭按钮,请按照下列步骤:1. Excel中创建新工作簿。2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。4. 将 CommandButton 控件添加到 UserForm。5. 双击以显示代码窗口对于 UserForm UserForm 。在代码窗口, 键入如下代码:Private Sub CommandButton1_Click()Unload MeEnd SubPrivate Sub UserForm_QueryClose(Cancel As Integer,6. CloseMode As Integer)IF CloseMode = vbFormControlMenu ThenCancel = TrueMe.Caption = Click the CommandButton to close Me!End IfEnd Sub7. 在运行菜单上,单击运行子过程/用户窗体。当您单击关闭按钮UserForm未关闭。您必须单击CommandButton控件关闭 UserForm。注意:代码包含在本文中不包含影响所有属性和对控件事件的示例。如果您不得不,请 使用属性窗口要查看可供控件属性的列表。要在视图菜单上,查看列表的属性,请单击属 性窗口。如何使用设计模式来编辑控件当您使用” VisualBasic编辑器来设计一个对话框,使用设计模式。在设计模式,您可编辑 控件和可更改属性在属性窗口 UserForm上的控制。若要显示属性窗口,在视图菜单上, 单击属性窗口。当您处在设计模式注意控件不响应与事件。当您运行一个对话框,显示方式,用户看 到它,程序处于运行模式。当UserForm是从内存中卸载将不会保留更改,对运行模式 中控件的属性。注意控件请回复到事件在运行模式。如何引用UserForm上控件如何您引用控件编程取决VisualBasic模块表运行代码的类型。如果代码从常规模块,运 行以下语法是:UserFormName.Controlname.Property=值例如,如果要设置名为TextBox,名为到值是Bob , UserForml UserForm上TextBox 控件的Text属性使用以下代码:UserForml.Tex tBoxl.Tex t 二Bob如果代码是通过事件的控件或者通过UserForm,启动过程中是您不需要引用名为UserForm。而,使用以下代码:Tex tBoxl.Tex t 二Bob当向对象,附加代码代码附加到之一为对象事件。众多,本文示例中,将代码附加到Click 事件是CommandButton对象。标签控件标签控件主要用于描述UserForm上其他控件。运行UserForm时Label控件不能编辑 由用户。使用Caption属性到设置或返回一个Label控件中文本。用于格式化Label控 件其他常用属性包括字体属性和ForeColor属性。如何使用WITH语句设置Label控件格式要使用WITH语句来更改属性的Label控件,请按照下列步骤:1. 启动Excel,并打开新空白工作簿。2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。4. 将 Label 控件添加到 UserForm。5. 将 CommandButton 控件添加到 UserForm。6. 双击以打开代码窗口对于 UserForm CommandButton 控件。7. 在代码窗口, 为 CommandButton 1 Click 事件键入下列代码:Private Sub CommandButton1_Click()With Label1 Set the text of the label.Caption = This is Label Example 1 Automatically size the label control.AutoSize = True.WordWrap = False Set the font used by the Label control.Font.Name = Times New Roman.Font.Size = 14.Font.Bold = True Set the font color to blue.ForeColor = RGB(0, 0, 255)End WithEnd Sub8. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。9. 单击 CommandButton 。文本粗TimesNewRoman用字体大小是14中Label控件上显示Thisis标签示例1VIOTextBox 控件TextBox控件经常用于收集来自用户输入。Text属性包含项,TextBox控件中进行。如何使用TextBox控件来验证密码如果您设置TextBox控件,PasswordChar属性的它成为masked -编辑控件。由字 符指定可视取代TextBox控件中键入的每个字符。要使用TextBox控件来验证密码,请 按照下列步骤:1. 启动Excel,并打开新空白工作簿。2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 O3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm O4. 将 TextBox 控件添加到 UserFormO5. 在 视图菜单上,单击 属性以显示属性窗口。对 TextBox 控件, PasswordChar 属性中键入 *6.注意您正将值改为星号。6. 将 CommandButton 控件添加到 UserFormO7. 双击以打开代码窗口对于UserForm CommandButton控件。 在代码窗口, 为 CommandButton 1 Click 事件键入下列代码:8. Private Sub CommandButton1_Click()If TextBox1.Text userform ThenMsgBox Password is Incorrect. Please reenter. TextBox1.Text = TextBox1.SetFocusElse MsgBox Welcome!Unload MeEnd If End Sub9. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。10. 在 TextBox 控件中键入密码 userform 。11. 单击 CommandButton 控件。对于本例,密码是” useform ”。如果您键入正确密码,您收到一个消息框,指出密码不 正确,然后重新键入密码可清除TextBox控件,并且。当您键入正确密码,收到欢迎消 息,并 UserForm 关闭。CommandButton 控件您可以使用CommandButton控制来启动VBA过程。VBA过程通常附加到CommandButton控件的Click事件。要使用CommandButton控件Click事件发生, 时,运行过程请按照步骤:1. 启动Excel,并打开新空白工作簿。2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。4. 将 CommandButton 控件添加到 UserForm。5. 双击以显示代码窗口对于 UserForm CommandButton 控件。 在代码窗口, 键入如下代码:Private Sub CommandButton1_Click()red = Int(Rnd * 255)6. green = Int(Rnd * 255)blue = Int(Rnd * 255)CommandButton1.BackColor = RGB(red, green, blue)End Sub7. 在运行菜单上,单击运行子过程/用户窗体。Comma ndButton 1控件的背景颜色更改每次您单击它。ListBox 控件ListBox控件的目的是为了向用户显示要选择的项目列表。您可以存储为Excel工作表上 ListBox控件项目列表。使用RowSource属性来填充工作表,上ListBox控件与范围的 单元格。ListBox控件在使用MultiSelect属性,时可设置为接受多重选择。如何从ListBox控件获取当前选定项使用Value属性的ListBox控件可返回当前选定项。要返回单项选择ListBox控件,中 当前选定项请按照下列步骤操作:1. 启动Excel,并打开新空白工作簿。2. 在单元格 A 1: A 5 Sheet, 键入了您要用于填充 ListBox 控件值。3. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。4. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。5. 将 ListBox 控件添加到 UserForm。6. 双击 ListBox 控件以显示代码窗口对 ListBox 控件。在代码窗口, 为 ListBox 1 Click 事件键入下列代码:Private Sub ListBox1_Click()7.MsgBox ListBox1.ValueEnd Sub8. 在运行菜单上,单击运行子过程/用户窗体。当单击列表,中的项目与当前选定项目将出现一个消息框。如何获取多选择ListBox控件中选定项确定多选择ListBox控件,中所选项目必须循环列表,中所有项目并再查询Selected属 性。要返回多选择,ListBox控件中当前选定项请按照下列步骤操作:1. 启动Excel,并打开新空白工作簿。2. 在单元格 A 1: A 5 Sheet, 键入了您要用于填充 ListBox 控件值。3. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。4. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。5. 将 ListBox 控件添加到 UserForm。6. 在 视图 菜单上, 单击 属性 以查看属性窗口。键入值, 对于下列 ListBox 控件属性表示:Property Value7.MultiSelectRowSource1 - frmMultiSelectMulti8.9.10.将 CommandButton 控件添加到 UserForm 。 双击以显示代码窗口对于 UserForm CommandButton 控件。 在代码窗口, 为 CommandButton 1 Click 事件键入下列代码 Sub CommandButton1_Click ()Sheet1!A1:A8 Loop through the items in the ListBox.For x = 0 to ListBox1.ListCount - 1 If the item is selected.If ListBox1.Selected(x) = True Then display the Selected item.MsgBox ListBox1.List(x)End IfNext xEnd Sub11. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。12. 列表中选择一个或多个项目。13. 单击 CommandButton 1 。单击CommandButton 1 ,后,在ListBox控件中选择每个项目显示在一个单独的消息 框。UserForm在消息框中,出现所有选定项后自动关闭。如何使用RowSource属性来填充工作表上以ListBox控件要使用RowSource属性来填充工作表,上ListBox控件从范围的单元格请按照下列步 骤:1. 启动Excel,并打开新空白工作簿。2. 在单元格 A 1: A 5 Sheet, 键入了您要用于填充 ListBox 控件值。3. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。4. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。5. 将 ListBox 控件添加到 UserForm。6. 将 CommandButton 控件添加到 UserForm。7. 双击以显示代码窗口对于 UserForm CommandButton 控件。 在代码窗口, 为 CommandButton 1 Click 事件键入下列代码: Private Sub CommandButton1_Click()ListBox1.RowSource = =Sheet1!A1:A5 End Sub在 运行 菜单上 , 单击 运行子过程 / 用户窗体 。9.注意 ListBox 1 不包含任何值。10. 单击 CommandButton 1 。ListBox 1填充单元格A 1: A 5 Sheet中有值。如何填充一个ListBox控件数组中有值下例显示您如何填充以数组ListBox控件。数组中每次为ListBox控件项必须分配值。 通常,此过程要求您使用循环结构,如ForaNext循环。要填充以数组,ListBox控件请按 照下列步骤操作:1. 启动Excel,并打开新空白工作簿。2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。4. 将 ListBox 控件添加到 UserForm。5. 在 插入 菜单上, 单击要插入模块表 模块 。在代码窗口, 键入如下代码:Sub PopulateListBox()Dim MyArray As VariantDim Ctr As IntegerMyArray = Array(Apples, Oranges, Peaches,6. Bananas, Pineapples)For Ctr = LBound(MyArray) To UBound(MyArray)UserForm1.ListBox1.AddItem MyArray(Ctr)NextUserForm1.ShowEnd Sub7. 然后单击运行在工具菜单上,、PopulateListBox ,和宏。PopulateListBox过程建立简单数组,并数组中通过使用Additem方法添加到ListBox控 件项目。然后,UserForm出现。如何使用工作表上水平的单元格区域来填充一个ListBox控件如果将ListBox控件的RowSource属性到水平区域的单元格,ListBox控件中第一个值 只会出现。要通过使用AddItem方法,ListBox控件从水平区域的单元格填充请按照下列步骤操作:1. 启动Excel,并打开新空白工作簿。2. 在单元格 A1:E1 Sheet, 键入了您要用于填充 ListBox 控件值。3. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。4. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。5. 将 ListBox 控件添加到 UserForm。6. 在 插入 菜单上, 单击要插入模块表 模块 。 在代码窗口, 键入如下代码:Sub PopulateListWithHorizontalRange()For Each x In Sheet1.Range(A1:E1)7. UserForm1.ListBox1.AddItem x.ValueNextUserForm1.ShowEnd Sub8. 然后单击 运行 在 工具 菜单上, 、 PopulateListWithHorizontalRange ,8. 和 宏 。在单元格A 1: E 5 Sheet,将值添加到ListBox 1 一次循环宏过程。A 1: E 5单元注意ListBox 1与不定Sheet 1上。如何从ListBox控件绑定到多列的数据返回多个值您可以格式ListBox控件以显示多个列的数据。这意味着ListBox控件,每个列表行上 显示多个项目。要多值列表,中选定项收益请按照下列步骤操作:1. 启动Excel,并打开新空白工作簿。Sheet 由该单元格中键入以下数据:A 1:年 B 1:区域C1:销售2. A 2: 1996B: 北美 C2: 1403:1996 B 3:南非C 3: 210A 4: 1997B4: 北美 C 4: 190A5: 1997 B 5:南非C 5: 1953. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。4. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。5. 将 Label 控件添加到 UserForm 。6. 将 ListBox 控件添加到 UserForm。7. 右击 ListBox , 然后单击 属性 。键入或选择值, 都表示为下列属性对 ListBox 控件与下表中列出: PropertyValue8.BoundColumnColumnCount ColumnHeads RowSource13TrueSheet1!A2:A59. 双击 ListBox 控件以显示代码窗口对 ListBox 控件。 在代码窗口, 键入如下代码:Private Sub ListBox1_Change()Dim SourceData As RangeDim Val1 As String, Val2 As String, Val3 As String Set SourceRange = Range(ListBox1.RowSource)10. Val1 = ListBox1.ValueVal2 = SourceRange.Offset(ListBox1.ListIndex,1) .Resize(1, 1).ValueVal3 = SourceRange.Offset(ListBox1.ListIndex,2) .Resize(1, 1).ValueLabel1.Caption = Val1 & & Val2 & & Val3 End Sub11. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。 当您单击ListBox控件,中一个条目标签将更改为显示该条目中所有三个项目。如何从绑定到工作表ListBox控件中删除所有项目要从ListBox控件绑定到工作表,删除所有项目清除,是存储在RowSource属性值。要 从ListBox控件绑定到工作表,删除项目请按照下列步骤:1. 启动Excel,并打开新空白工作簿。2. 在单元格 A 1: A 5 Sheet, 键入了您要用于填充 ListBox 控件值。3. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。4. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。5. 将 ListBox 控件添加到 UserForm。6. 用鼠标右键单击 ListBox 控件, 然后单击 属性 。7. 在 RowSource 属性, 键入 Sheet 1 A 1: A 5 !8. 将 CommandButton 控件添加到 UserForm。9. 双击以显示代码窗口为 CommandButton 控件 CommandButton 控件。 在代码窗口, 为 CommandButton 1 Click 事件键入下列代码: Private Sub CommandButton1_Click()10.ListBox1.RowSource = End Sub在 运行 菜单上 , 单击 运行子过程 / 用户窗体 。11.ListBox 控件, 添加到 UserForm 具有值, 您输入 Sheet 填充。12. 单击 CommandButton 1 。从ListBox 1中删除所有项目。如何从不绑定到工作表ListBox控件中删除所有项目没有没有单个VBA命令如果没有绑定到工作表列表,从ListBox控件删除所有项目。要 从ListBox控件从VisualBasic数组,填充删除所有项目请按照下列步骤:1. 启动Excel,并打开新空白工作簿。2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。4. 将 ListBox 控件添加到 UserForm。5. 在 插入 菜单上, 单击要插入模块表 模块 。在代码窗口, 键入如下代码:Sub PopulateListBox()Dim MyArray As VariantDim Ctr As Integer6. MyArray = Array(Apples, Oranges, Peaches, Bananas, Pineapples)For Ctr = LBound(MyArray) To UBound(MyArray) UserForm1.ListBox1.AddItem MyArray(Ctr)NextUserForm1.ShowEnd Sub7. 将 CommandButton 控件添加到 UserForm。8. 双击以显示代码窗口为 CommandButton 控件 CommandButton 控件 在代码窗口, 为 CommandButton 1 Click 事件键入下列代码: Private Sub CommandButton1_Click()For i = 1 To ListBox1.ListCount9.ListBox1.RemoveItem 0Next IEnd Sub然后单击 运行 在 工具 菜单上,、 PopulateListBox , 和 宏10.ListBox 控件填充, 并再出现 UserForm。10. 单击 CommandButton 1从ListBox 1中删除所有项目。ComboBox 控件您可以使用ComboBox控件作为在下拉列表框中,或组合框其中您可选择列表中值或键 入新值。Style属性决定如果ComboBox控件作为下拉列表框或组合框。注意前述对ListBox控件中所有示例也能应用到ComboBox控件,除例如获取多选择ListBox控件中选定项如何”。如何向列表添加新项目如果ComboBox控件未绑定到工作表键入值是未在列表中ComboBox控件,时可能要向列表添加新值。要添加新值,如果 ComboBox控件未绑定到工作表,ComboBox控件中键入请按照下列步骤操作:1. 启动Excel,并打开新空白工作簿。2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。4. 将 ComboBox 控件添加到 UserForm。5. 在 插入 菜单上, 单击要插入模块表 模块 。在代码窗口, 键入如下代码:Sub PopulateComboBox()Dim MyArray As Variant6. Dim Ctr As IntegerMyArray = Array(Apples, Oranges, Peaches, Bananas, Pineapples)For Ctr = LBound(MyArray) To Ubound(MyArray)UserForm1.ComboBox1.AddItem MyArray(Ctr)NextUserForm1.ShowEnd Sub7. 将 CommandButton 控件添加到 UserForm。8. 双击以显示代码窗口为 CommandButton 控件 CommandButton 控件。 在代码窗口, 为 CommandButton 1 Click 事件键入下列代码: Private Sub CommandButton1_Click()Dim listvar As Variantlistvar = ComboBox1.ListOn Error Resume Next If the item is not found in the list.9.If IsError(WorksheetFunction.Match(ComboBox1.Value, listvar, 0) Then add the new value to the list.ComboBox1.AddItem ComboBox1.ValueEnd IfEnd Sub然后单击 运行 在 工具 菜单上,、 PopulateListBox , 和 宏 。10.填充 组合框 控件, 并再出现 UserForm。11. ComboBox 控件, 中键入 Mangoes (或任何值尚未是列表中)。11. 单击 CommandButton 1 。现在您键入新值将在列表末尾。如何向列表添加新项目如果ComboBox控件绑定到工作表当用户键入值是未在列表中ComboBox控件,可能需要新值添加到列表。要添加该列表, ComboBox控件中键入新值请按照下列步骤操作:1. 启动Excel,并打开新空白工作簿。2. 单元格 A 1: A 5 Sheet, 中键入值与要用于填充 组合框 控件。3. 选择单元格 A 1: A 5 Sheet 1 上。插入 菜单上指向 名称 , 然后单击 定义 。4. 在 工作簿中名称 框中, 键入 ListRange , 然后单击 确定 。 这创建 ListRange 定义名称。 使用定义名称 ListRange 将 ComboBox 控件的 RowSource 属性绑定到工作表。5. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。6. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。7. 将 ComboBox 控件添加到 UserForm。8. 对于 ComboBox 1 , 属性 中键入 与 RowSource 属性 ListRangeSheet1!9. 将 CommandButton 控件添加到 UserForm。10. 双击以显示代码窗口为 CommandButton 控件 CommandButton 控件。在代码窗口, 为 CommandButton 1 Click 事件键入下列代码: Private Sub CommandButton1_Click()Dim SourceData As RangeDim found As ObjectSet SourceData = Range(ListRange)Set found = Nothing Try to find the value on the worksheet.Set found = SourceData.Find(ComboBox1.Value) If the item is not found in the list. If found Is Nothing Then11. redefine ListRange. SourceData.Resize(SourceData.Rows.Count + 1, 1).Name =ListRange Add the new item to the end of the list on the worksheet. SourceData.Offset(SourceData.Rows.Count, 0).Resize(1, 1).Value _= ComboBox1.Value Reset the list displayed in the ComboBox. ComboBox1.RowSource =Range(listrange).Address(external:=True)End IfEnd Sub在 运行 菜单上 , 单击 运行子过程 / 用户窗体 。12.UserForm 出现在 Sheet 1。12. ComboBox 控件, 中键入值尚未列表中。13. 单击 CommandButton 1 。ComboBox控件中键入新项目添加到列表,并范围扩展到包括单元A1:A6, ComboBox 控件绑定到列表。当出现UserForm如何显示ComboBox控件列表有时,可能非常有用以UserForm首次出现时显示ComboBox控件的列表。以下示例使 用是UserForm Activate事件。要显示的ComboBox控件,列表请按照下列步骤操作:1. 启动Excel,并打开新空白工作簿。2. 单元格 A 1: A 5 Sheet, 中键入值与要用于填充 组合框 控件。3. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。4. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。5. 将 ComboBox 控件添加到 UserForm。6. 对于 ComboBox 1 , 属性 中键入 Sheet 1 与 RowSource 属性 A 1: A 5 !7. 双击以显示代码窗口对于 UserForm UserForm 。8. 在代码窗口, 为 CommandButtonClick 事件键入下列代码:Private Sub UserForm_Activate()ComboBox1.DropDownEnd Sub9. 在 运行 菜单上, 单击 运行子过程 / 用户窗体UserForm, Sheet 1上出现,您可看到该列表对于ComboBox 1。当其他ComboBox控件中进行选择如何显示一个ComboBox控件列表 要,ComboBox控件中进行选择时自动显示一个ComboBox控件的列表请按照下列步骤 操作:1. 启动Excel,并打开新空白工作簿。2. 在单元格 A 1: A 10 Sheet, 输入值与要用于填充 组合框 控件。3. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。4. 在 插入 菜单上, 单击 模块 。 在代码窗口为模块, 键入如下代码:Sub DropDown_ComboBox()5.UserForm1.ComboBox2.DropDownEnd Sub5. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。6. 将 ComboBox 控件添加到 UserForm。7. 对于 ComboBox 1 , 属性 中键入 Sheet 1 与 RowSource 属性 A 1: A 58. 双击要打开代码窗口对 ComboBox 控件 ComboBox 控件。在代码窗口对 ComboBox 控件, 为 ComboBox Click 事件键入下列代码: Private Sub ComboBox1_Click()10.Application.OnTime Now, DropDown_ComboBoxEnd Sub9. 添加到 UserForm 二 ComboBox 控件。10. 对于 ComboBox2 , 属性 中键入 Sheet 1 与 RowSource 属性 A6:A1011. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。对于ComboBox2列表单击,ComboBox 1列表中的项目时将自动出现。框架控件使用Frame控件来分组UserForm中逻辑相关项。框架控件经常用于分组OptionButton 控件。如何循环Frame控件上的所有控件要使用EachaNext For循环来访问框架控件,中所有控件请按照下列步骤:1. 启动Excel,并打开新空白工作簿。2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。4. 将 Frame 控件添加到 UserForm。 OptionButton 控件添加到 Frame 控件。5. 重复此步骤向 Frame 控件中添加两个详细 OptionButton 控件。6. 双击要打开代码窗口对 Frame 控件 Frame 控件。 在代码窗口, 为 框架 Click 事件键入下列代码:Private Sub Frame1_Click()Dim Ctrl As Control7. For Each Ctrl In Frame1.Controls Ctrl.Enabled = Not Ctrl.Enabled Next End Sub8. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。9. 在 UserForm,单击 Frame 控件。首次您单击框架控件,Frame控件中所有控件是不可用。如果再次,单击Frame控件 控件可再次。OptionButton 控件可使用OptionButton控件组进行一个选择的选项组中。使用以下技术到组OptionButton 控件之一:?框架控件? GroupName 属性注意On值,是值,和True值表明已选中一个OptionButton。Off值、无值, 和False值表明未选中OptionButton攻击。如何确定当OptionButton控件位于Frame控件被选中OptionButton控 件通过使用Frame控件,Opti on Butt ons控件分组时您可以确定通过循环Frame控件中所 有控件并检查Value属性的每个控件是选定OptionButton控件。要确定所选,OptionButton控件请按照下列步骤:1. 启动Excel,并打开新空白工作簿。2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。4. 将 Frame 控件添加到 UserForm。OptionButton 控件添加到 Frame 控件。5.重复此步骤向 Frame 控件中添加两个详细 OptionButton 控件。5. 添加一个 CommandButton 控件 UserForm 之外 Frame 控件上。6. 双击以显示代码窗口对于 UserForm CommandButton 控件。 在代码窗口, 为 CommandButton 1 Click 事件键入下列代码 Private Sub CommandButton1_Click()For Each x In Frame1.Controls7. If x.Value = True ThenMsgBox x.CaptionEnd IfNextEnd Sub8. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。9. UserForm , 中一个 OptionButton 控件, 依次 CommandButton 1 。将出现一个消息框包含当前选定OptionButton控件的题注。如何确定所选OptionButton控件对以下示例目的是为了确定Groupl中选定OptionButton控件。若要创建具有两个OptionButton控件组,UserForm请按照下列步骤:1. 启动Excel,并打开新空白工作簿。2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。4. 将 Frame 控件添加到 UserForm。Frame 控件中添加一个 OptionButton 控件。5.重复此步骤向 Frame 控件中添加两个详细 OptionButton 控件。5. 对于每个 OptionButton 控件, 在 GroupName 属性键入 Group1 。6. 重复步骤 4 和 5 以创建包含三个 OptionButton 控件二 Frame 控件。7. 对于每个 OptionButton 控件在第二个 框架 控件, 在 GroupName 属性键 入 Group2 。8. 添加一个 CommandButton 控件 UserForm 之外 Frame 控件上。9. 双击以显示代码窗口对于 UserForm CommandButton 控件。 在代码窗口, 为 CommandButton 1 Click 事件键入下列代码: Private Sub CommandButton1_Click()Dim x As Control Loop through ALL the controls on the UserForm. For Each x In Me.Controls Check to see if Option is in the Name of each control. If InStr(x.Name, Option) Then Check Group name. If x.GroupName = Group1 Then Check the status of the OptionButton.If x.Value = True ThenMsgBox x.CaptionExit ForEnd IfEnd IfEnd IfNextEnd Sub12. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。13. UserForm, 中 Group1, 中一个 OptionButton 控件依次 CommandButton 13.1 。丄 o将出现一个消息框包含OptionButton控件当前选定的标题。CheckBox 控件您可以使用CheckBox控制来指示真或假值。带有复选标记中出现CheckBox控件指示值为True。与没有复选标记出现CheckBox表示值为False。如果TripleState属性的值为True , CheckBox控件还可以Null值。可用似乎具有Null值CheckBox控件。注意On值,是值,和True值表明,CheckBox控件被选定。Off值、无值,和False值表明CheckBox控件已清除。如何检査CheckBox控件的值要使用Value属性以返回当前值的CheckBox控件,请按照下列步骤:1. 启动Excel,并打开新空白工作簿。2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。4. 将 CheckBox 控件添加到 UserForm。5. 在属性列表对于 CheckBox 1 , 选择 True 与 TripleState 属性。6. 双击 CheckBox 控件以显示代码窗口为 CheckBox 控件。在代码窗口, 为 CheckBox 1 Change 事件键入下列代码:Private Sub CheckBox1_Change()Select Case CheckBox1.ValueCase TrueCheckBox1.Caption = True7. Case FalseCheckBox1.Caption = FalseC
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 解决方案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!