如何创建ActiveX控件命令按钮并将其设置为变量Excel VBA [英] How to create and set a ActiveX Control CommandButton as a variable Excel VBA

查看:93
本文介绍了如何创建ActiveX控件命令按钮并将其设置为变量Excel VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Excel VBA创建新的ActiveX控件命令按钮.我有一个过去工作过的循环VBA,theFile1.1.xlsm具有工作簿的主列表.我需要在〜3200个工作簿中添加一个CommandButton,因此我将使用Do-Loop宏.这是循环代码供参考.

I am trying to Create a New ActiveX Control CommandButton with Excel VBA. I have a loop VBA which has worked in the past, theFile1.1.xlsm has the master list of the workbooks. I need to add a CommandButton to ~3200 workbooks, so I will be using the Do-Loop macro. Here is the Loop code for reference.

Sub Macro2() 

Application.ScreenUpdating = False

Dim sFile As String
Dim wb As Workbook
Dim FileName1 As String
Dim FileName2 As String
Dim wksSource As Worksheet
Const scWkbSourceName As String = "theFILE 1.1.xlsm"

Set wkbSource = Workbooks(scWkbSourceName)
Set wksSource = wkbSource.Sheets("Sheet1") ' Replace 'Sheet1' w/ sheet name of SourceSheet

Const wsOriginalBook As String = "theFILE 1.1.xlsm"
Const sPath As String = "E:\ExampleFolder\"

SourceRow = 5

Do While Cells(SourceRow, "D").Value <> ""

    Sheets("Sheet1").Select

    FileName1 = wksSource.Range("A" & SourceRow).Value
    FileName2 = wksSource.Range("K" & SourceRow).Value

    sFile = sPath & FileName1 & "\" & FileName2 & ".xlsm"

    Set wb = Workbooks.Open(sFile)

        ''insert code for loop operation

    '''CLOSE WORKBOOK W/O BEFORE SAVE
    Application.EnableEvents = False
    ActiveWorkbook.Save
    ActiveWorkbook.Close
    Application.EnableEvents = True

SourceRow = SourceRow + 1

Loop

End Sub

我想将按钮设置为变量(我认为),因此我可以编辑格式/属性,并希望以后向该按钮添加宏.

I would like to have the button set as a Variable (i think), so I can edit the formatting/properties and hopefully add a macro to the button later.

    Dim buttonControl As MSForms.CommandButton

    Set buttonControl = _
        ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
        Link:=False, _
        DisplayAsIcon:=False, _
        Left:=1464, Top:=310, Width:=107.25, Height:=30)

    With buttonControl.Opject
        .Caption = "OPEN FOLDER"
        .Name = "cmd_OPEN_FOLDER"

    End With

我遇到运行时错误13:类型不匹配"错误.我不确定为什么,因为在正确的位置创建了"CommandButton1".

I have a 'Run-time error 13: Type Mismatch' error. I am unsure why, because a 'CommandButton1' is created in the correct place.

推荐答案

OLEObjects.Add 创建一个 OLEObject 并将其添加到 OLEObjects 收藏;由 Add 函数返回的对象是 OLEObject ,而不是 MSForm.CommandButton .这是 OLEObject.Object 的基础类型-因此,请将您的 buttonControl 设置为返回对象的 .Object 属性:

OLEObjects.Add creates an OLEObject and adds it to the OLEObjects collection; the object returned by the Add function is OLEObject, not MSForm.CommandButton. That's the underlying type of OLEObject.Object - so, set your buttonControl to the .Object property of the returned object:

Set buttonControl = _
    ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
    Link:=False, _
    DisplayAsIcon:=False, _
    Left:=1464, Top:=310, Width:=107.25, Height:=30).Object

在正确的位置创建了按钮,因为 Add 函数可以工作并返回-类型不匹配的失败之处在于将返回的 OLEObject 分配给了 CommandButton 变量,紧接在该操作之后.

The button is created in the correct place, because the Add function works and returns - what's failing with a type mismatch is the assignment of the returned OLEObject into a CommandButton variable, immediately after that operation.

随后的 With 块可以只是 With buttonControl .

这篇关于如何创建ActiveX控件命令按钮并将其设置为变量Excel VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆