Excel VBA CommandBar.OnAction with params is difficult / does not perform as expected

后端 未结 3 966
误落风尘
误落风尘 2021-01-19 01:09

So, I have Googled about and it seems that while making custom Pop up menus, if one wants to pass parameters then this is possible but for me comes with 2 major pro

3条回答
  •  迷失自我
    2021-01-19 01:35

    You can use the .Parameter property. This is an example of a code in production (with only the lines of interest):

            Dim i As Integer
            Set cl = MainForm.Controls("classroomList")
            For i = 0 To cl.ListCount - 1
                With .Controls.Add(Type:=msoControlButton)
                    .Caption = cl.List(i)
                    .faceId = 177
                    .OnAction = "'" & ThisWorkbook.Name & "'!" & "assignClassroom"
                    .Parameter = cl.List(i)
                End With
            Next i
    

    And the procedure could be something like:

    Public Sub assignClassroom(Optional someArg as SomeType)
    ' code here
    CommandBars.ActionControl.Parameter 'The parameter here
    ' more code here
    End Sub
    

提交回复
热议问题