1

我正在处理具有两个命令按钮的代码:

1) 用户输入

2) 执行

单击时,用户输入按钮使用户窗体出现。根据 UserForm 输入,工作表格式会调整,用户会在提示时将数据输入到工作表中。执行按钮执行计算并填写工作表的其余部分,然后绘制结果图表或打开一个新工作表,然后包含相同的两个按钮。

我可以创建新工作表,但工作表只包含一个命令按钮。我的代码如下:

Dim obj As Object
Dim Code As String
Dim obj2 As Object
Dim code2 As String

    With Sec_Delay

        Set obj = .OLEObjects.Add(classType:="Forms.CommandButton.1", _
                                Link:=False, DisplayAsIcon:=False, Left:=279, _
                                Top:=210.75, Width:=109.5, Height:=24)
        obj.Name = "ButtonTest"
        obj.Object.Caption = "USER INPUT"

        Code = "Sub ButtonTest_Click()" & vbCrLf & _
            "UF_input.Show" & vbCrLf & _
            "End Sub"

        Set obj2 = .OLEObjects.Add(classType:="Forms.CommandButton.2", _
                                Link:=False, DisplayAsIcon:=False, Left:=277.5, _
                                Top:=236.25, Width:=111, Height:=24)
        obj2.Name = "Execute2Test"
        obj2.Object.Caption = "Execute"

        code2 = "Sub Execute2Test_Click()" & vbCrLf & _
                "Cells(8,1) = 1" & vbCrLf & _
                "End Sub"

        With .Parent.VBProject.VBComponents(.CodeName).CodeModule
            .insertlines .CountOfLines + 1, Code
        End With
    End With

此代码在我创建新工作表的子目录中。新工作表称为“Sec_Delay”,它只有一个命令按钮。我从stackoverflow的其他地方提取了第一个命令按钮的代码,所以我不熟悉最后一部分的作用:

    With .Parent.VBProject.VBComponents(.CodeName).CodeModule
        .insertlines .CountOfLines + 1, Code
    End With

但我主要了解 OLEObject 的工作原理。我只是不明白如何为新工作表制作第二个命令按钮。

如何创建第二个命令按钮?为什么“Forms.CommandButton.2”什么都不做?无论如何,“.1”是什么意思?是否可以在同一个 sub 中有两个 OLEObjects?

4

1 回答 1

1

您快到了...

“Forms.CommandButton.1”是控件的名:它决定了创建什么类型的控件,您不应该更改该值,否则 excel 将无法识别它。

代码的最后一部分将按钮的事件处理程序添加到工作表的代码模块:创建工作表后,您可以在 VB 编辑器中查看代码。

Dim obj As Object
Dim Code As String
Dim obj2 As Object
Dim code2 As String

With Sec_Delay

    Set obj = .OLEObjects.Add(classType:="Forms.CommandButton.1", _
                            Link:=False, DisplayAsIcon:=False, Left:=279, _
                            Top:=210.75, Width:=109.5, Height:=24)
    obj.Name = "ButtonTest"
    obj.Object.Caption = "USER INPUT"

    Code = "Sub ButtonTest_Click()" & vbCrLf & _
        "UF_input.Show" & vbCrLf & _
        "End Sub"

    'edit: use "Forms.CommandButton.1" again
    Set obj2 = .OLEObjects.Add(classType:="Forms.CommandButton.1", _
                            Link:=False, DisplayAsIcon:=False, Left:=277.5, _
                            Top:=236.25, Width:=111, Height:=24)
    obj2.Name = "Execute2Test"
    obj2.Object.Caption = "Execute"

    code2 = "Sub Execute2Test_Click()" & vbCrLf & _
            "Cells(8,1) = 1" & vbCrLf & _
            "End Sub"

    With .Parent.VBProject.VBComponents(.CodeName).CodeModule
        .insertlines .CountOfLines + 1, Code
        .insertlines .CountOfLines + 1, code2 '<< added
    End With
End With

编辑:如果要从常规模块调用位于工作表模块中的代码,则需要在调用中包含模块名称。

Sub TestCall()
    Sheet1.Tester
End Sub

...并确保您使用的是工作表的代号,而不是选项卡名称:

在此处输入图像描述

于 2018-04-26T20:38:11.747 回答