简体   繁体   English

使用controlpopup vba运行宏

[英]Run macro using controlpopup vba

I am trying to write a macro that will create a popup window that will allow me to navigate between pages of a workbook. 我正在尝试编写一个宏,该宏将创建一个弹出窗口,使我可以在工作簿的页面之间导航。 So far I have been able to populate a controlpopup that runs a simple macro, but I would like it to be able to accept variables (aka the sheetname) to avoid having to write a bunch of different macros. 到目前为止,我已经能够填充运行一个简单宏的controlpopup,但是我希望它能够接受变量(又称工作表名称),而不必编写许多不同的宏。 Here is what I have so far. 这是我到目前为止所拥有的。

Sub Custom_PopUpMenu_1(Mname As String)
    Dim MenuItem As CommandBarPopup
    ' Add the popup menu.
    With Application.CommandBars.Add(Name:=Mname, Position:=msoBarPopup, _
         MenuBar:=False, Temporary:=True)

        With .Controls.Add(Type:=msoControlPopup)
            .Caption = "Menu Control"
              With .Controls.Add(Type:=msoControlButton)
                .Caption = "Button 1 in menu"
                .FaceId = 71
                .OnAction = "'" & ThisWorkbook.Name & "'!" & "TestMacro"
            End With
        End With 
    End With
End Sub

This runs the macro 'TestMacro' but am not sure how to write it so that it will accept a string variable. 这将运行宏“ TestMacro”,但不确定如何编写宏,以便它可以接受字符串变量。 My other though is that in each .OnAction to update a global variable that is the sheetname, but am unsure of how to do that either. 我的另一个想法是,在每个.OnAction中,更新工作表名称的全局变量,但不确定如何执行此操作。 Let me know if you can help out. 让我知道您是否可以帮忙。 Thanks! 谢谢!

Your testmacro should launch a userform which would have a ComboBox and a CommandButton 您的testmacro应该启动一个用户testmacro ,该testmacro具有一个ComboBox和一个CommandButton

The logic is, when the userform initializes, it will store all the sheet names from the current workbook in the ComboBox and you can use the CommandButton to navigate to that sheet. 逻辑是,当用户窗体初始化时,它将把当前工作簿中的所有工作表名称存储在ComboBox ,您可以使用CommandButton导航到该工作表。

Here is an Example 这是一个例子

Private Sub UserForm_Initialize()
    Dim ws As Worksheet

    For Each ws In ActiveWorkbook.Worksheets
        ComboBox1.AddItem ws.Name
    Next ws
End Sub

Private Sub CommandButton1_Click()
    Dim wsName As String

    If ComboBox1.ListIndex <> -1 Then
        wsName = ComboBox1.List(ComboBox1.ListIndex)

        If ActiveWorkbook.Sheets(wsName).Visible <> xlSheetVisible Then
            ActiveWorkbook.Sheets(wsName).Visible = True
        End If

        ActiveWorkbook.Sheets(wsName).Activate
    End If
End Sub

ScreenShot 截图

在此处输入图片说明

This should work (at least, it works for a shape on a worksheet) 这应该有效(至少,它适用于工作表上的形状)

.OnAction = "'" & ThisWorkbook.Name & "'!'TestMacro ""blah22""'"

...but if what you want is to navigate to a particular sheet then right-clicking on the "VCR" control to the left of the sheet tabs is the easiest built-in way... ...但是如果您要导航到特定的工作表,则右键单击工作表标签左侧的“ VCR”控件是最简单的内置方法...

If your parameter is in a variable you can do this: 如果您的参数在变量中,则可以执行以下操作:

.OnAction = "'" & ThisWorkbook.Name & "'!'TestMacro """ & stringvariable  & """'"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM