excel动态创建控件

来源:互联网 发布:淘宝助理5.4版本下载 编辑:程序博客网 时间:2024/05/29 04:01
分配上单击VBA函数到一个Excel的Userform中动态创建的按钮 Sign InTop AuthorsTerms of ServiceContact UsBlog 
.htaccessAdmin 
Home    English Language Content







分配上单击VBA函数到一个Excel的Userform中动态创建的按钮
我动态创建的按钮在Excel用户窗体用下面的代码: 

With Me.CurrentFrame.Controls.Add("Forms.CommandButton.1")     .Caption = "XYZ"     .name = "AButton"     .Font.Bold = True     .ForeColor = &HFF&     ... blah blah blah End With 我想指定一个函数来运行这些按钮被点击时,但我不能找到一个直接的方式做到这一点,因为那里没有财产的按钮本身的一部分。 

有没有办法做到这一点使用上述成语? 我应该在这整个事情用不同的方式去? 

#: 566770     excel    vba    userform    event-handling     User: notnot 
Answer: 566965
你需要动态创建代码为每个按钮/事件处理程序。 

它需要做位-在这里看到: http://navpadexcel.blogspot.com/2006/11/httpwwwcpearsoncomexcelvbehtm.html 

一个更好的办法可能是建立一个按钮的形式(如你认为你会需要很多)提前一堆。 创建事件处理程序代码以及。 使他们所有隐藏的最初。 

然后,当你打开你的形式可以动态改变按钮的标题,使他们看到并四处移动它们。 事件代码创建初期将与激活的按钮按预期。 

#: 566965     excel    vba    userform    event-handling     User: DJ 
Comments for #: 566965
聪明的主意! 似乎一切都需要在VBA被聪明。   #:378249  notnot  
Answer: 1517920
下面应该努力 

Dim NewButton As OLEObject Dim CodeModule As Object Set NewButton = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _     Link:=False, DisplayAsIcon:=False, Left:=52.5, Top:=Hght, _     Width:=202.5, Height:=26.25) NewButton.Object.Caption = "Click Me!" Set CodeModule = ActiveWorkbook.VBProject.VBComponents.VBE.ActiveCodePane.CodeModule CodeModule.InsertLines CodeModule.CreateEventProc("Click", NewButton.Name) + 1, vbTab & "MsgBox ""Hello world""" 

#: 1517920     excel    vba    userform    event-handling     User: Alex 
Answer: 971866
我一直在看这一点。 看来你可以通过使用的onClick属性的宏: 

Command1.OnClick = "Macro1" 然后创建一个以该名称,运行所需的功能的宏。 
这是我劈解决此之前,我找到更好的。 

#: 971866     excel    vba    userform    event-handling     User: n/a 
Answer: 567117
网络上的每一个搜索归结到这个代码: 

Dim NewButton As Control    Dim CodeModule As Object   Set NewButton = Me.Controls.Add("Forms.CommandButton.1", "MyButton", True)   NewButton.Caption = "Click me!"   Set CodeModule = ActiveWorkbook.VBProject.VBComponents.VBE.ActiveCodePane.CodeModule   CodeModule.InsertLines CodeModule.CreateEventProc("Click", NewButton.Name) + 1, vbTab & "MsgBox ""Hello world""" 不过,我已尝试了所有的例子和无法让他们的工作。 

#: 567117     excel    vba    userform    event-handling     User: Stefan 
Search
   Bookmark
Related Articles
Topic: event-handling
Raising C# events with an extension method - is it bad?
event handler problems c#
Fake "click" to activate an onclick method
install EventFilter on QWidget (qt4.4.3/kde4)
Override Button_click event handler
C#: Difference between ' += anEvent' and ' += new EventHandler(anEvent)'Topic: excel
How do you get a spreadsheet to open Excel instead of a browser window?
Create Excel (.XLS and .XLSX) file from C#
C# Technique - Getting Constant Values By String
.Net Excel Add In Help
c# How to access an excel cell?
Non-Sequential RangeTopic: userform
VBA UserForm ObjectTopic: vba
How can I URL encode a string in Excel VBA?
Is there any way to authenticate user in a ms-access database?
access vba: Runtime error 3734
Importing/Exporting Relationships in MS Access
Making Excel macro for fast data entering ::: VBA ::: Excel :::
Show Cell Range on UserForm; then updateIndex
Map: db
so - Database
sf - Database
su - DatabaseMap: international
de. German
es. Spanish
fr. French
id. Indonesian
it. Italian
ja. Japanese
ko. Korean
pt. Portuguese
ru. Russian
th. Thai
vi. Vietnamese
zh-cn. Chinese
zh-tw. ChineseMap: sitemap
Update Sitemap
Page: «
Page: 939
Page: 940
Page: 941
Page: 942
Page: 943
Page: 944
Page: 945
Page: 946
Page: 947
Page: 948
Page: 949
Page: 950
Page: 951
Page: 952
Page: 953
Page: 954
Page: 955
Page: 956
Page: 957
Page: 958
Page: 959
Page: 960
Page: 961
Page: 962
Page: 963
Page: 964
Page: 965
Page: 966
Page: 967
Page: » 
Automagic Translation
0.2338.3.41.22.11.23  Dump from StackOverflow 
0 0
原创粉丝点击