欢迎来到天天文库
浏览记录
ID:28565288
大小:133.04 KB
页数:5页
时间:2018-12-11
《excel vba 窗体批量创建插件(按钮,标签,单选......)》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、在excel文件工作表中创建按钮插件,按钮名称为CommandButton1,如下所示:粘贴下面的代码至创建按钮工作表后台书写代码处,点击按钮如下所示:PrivateSubCheckBox1_Click()EndSubPrivateSubCommandButton1_Click()DimTempFormDimLeftPosAsIntegerDimXAsIntegerDimiAsIntegerDimTopPosAsInteger'创建窗体SetTempForm=ActiveWorkbook.VBProject.VBComponents.Add(
2、3)'声明创建窗体插件DimNewOptionButtonAsMSForms.OptionButtonDimnewCommandButtonnAsMSForms.CommandButtonDimnewCheckBoxAsMSForms.CheckBoxDimnewLabelAsMSForms.LabelLeftPos=4k=1TopPos=5'循环创建单选框个数(可根据实际情况而定)Fori=1To10LeftPos=4'创建单选框SetNewOptionButton=TempForm.Designer.Controls.Add("forms
3、.OptionButton.1")'设置单选框属性WithNewOptionButton.Width=60.Caption=k&"℃".Height=15.Left=LeftPos.Top=TopPos.Tag=k&"℃".AutoSize=TrueEndWithLeftPos=LeftPos+30k=k+1TopPos=i*20+5'创建单选框宏代码WithTempForm.CodeModuleX=.CountOfLines.InsertLinesX+1,"PrivateSubOptionButton"&i&"_Click()".Inser
4、tLinesX+2,"me.Label1.caption="""+"你的选择:"+""+CStr(i)+""+"℃"+"""".InsertLinesX+3,"EndSub"EndWithNexti'创建按钮(可以批量创建)SetnewCommandButtonn=TempForm.Designer.Controls.Add("Forms.CommandButton.1")'设置按钮属性newCommandButtonn.Name="MyCommandButton"newCommandButtonn.Object.Caption="确定"Wi
5、thnewCommandButtonn.Width=60.Height=20.Left=LeftPos.Top=TopPos+40.AutoSize=FalseVisible=TrueEndWith'创建标签(可以批量创建)SetnewLabel=TempForm.Designer.Controls.Add("Forms.Label.1")'设置标签属性newLabel.Caption=""WithnewLabel.Width=120.Height=20.Left=LeftPos.Top=TopPos.AutoSize=FalseVisibl
6、e=TrueEndWith'设置创建按钮宏代码WithTempForm.CodeModuleX=.CountOfLines.InsertLinesX+1,"PrivateSubMyCommandButton_Click()".InsertLinesX+2,"msgboxMe.Label1.Caption+me.check.caption".InsertLinesX+3,"me.hide".InsertLinesX+4,"endsub"EndWithSetnewCheckBox=TempForm.Designer.Controls.Add("F
7、orms.CheckBox.1")newCheckBox.Caption="java"newCheckBox.Name="check"WithnewCheckBox.Width=120.Height=20.Left=LeftPos.Top=TopPos+20.AutoSize=FalseVisible=TrueEndWith'设置窗体属性WithTempForm.Properties("Caption")="窗体界面".Properties("Width")=LeftPos+200.Properties("Height")=TopPos+10
8、0.Properties("Left")=160.Properties("Top")=100EndWith'显示窗体VBA.UserForms.Add(TempFo
此文档下载收益归作者所有