How to change the caption of multiple button instances in excel vba

纵然是瞬间 提交于 2020-05-17 08:49:24

问题


With this code, I have created multiple instances of button using copy and paste but when I change the caption of second button, it is changing the caption of first button

Sub CopyActiveX(address)
    Application.ScreenUpdating = False
    Dim X As OLEObject, Y As OLEObject
    Dim newButtonName As String
    Dim count As Integer

    count = 0
    For Each X In ActiveSheet.OLEObjects
        count = count + 1
      Debug.Print X.Name
    If X.Name = "btnFindSeries" Then
       newButtonName = X.Name + "" + CStr(count)
    End If
    Next X

    Set X = Sheets("Sheet1").OLEObjects("btnFindSeries")

    Set Y = X.Duplicate
    Dim xName As String
    xName = X.Name
    Y.Cut
    With Sheets("Sheet1")
        .Paste Destination:=Worksheets("Sheet1").Range(address)
        .OLEObjects(.OLEObjects.count).Name = newButtonName
        .Activate
    End With
    AddSheetEventButMouseDown newButtonName
    Application.ScreenUpdating = True
End Sub

Caption: Called using .OnAction = "SetCaption" of type msoControlButton from context menu

Public Sub SetCaption()
    NavigatorType = CommandBars.ActionControl.Parameter
    ActiveSheet.OLEObjects("btnFindSeries").Object.Caption = NavigatorType
End Sub

I know that the above code is updating only on button caption

but how do I dynamically get reference to second button and update caption when clicked on context menu?

来源:https://stackoverflow.com/questions/61843910/how-to-change-the-caption-of-multiple-button-instances-in-excel-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!