Excel 2010 - Error: Cannot run the macro SelectCell using .onAction

后端 未结 2 709
星月不相逢
星月不相逢 2021-01-22 01:47

I have been looking all over the internet for a solution to this problem but for some reason I can never find anything directly related to using .onAction with selecting a speci

相关标签:
2条回答
  • 2021-01-22 02:11

    This (both subs in a regular module) works for me.

    Sub SelectCell(sht As String, rng As String)
        ThisWorkbook.Sheets(sht).Range(rng).Select
    End Sub
    
    Sub Assign()
        ActiveSheet.Shapes(1).OnAction = "'SelectCell """ & _
                Selection.Parent.Name & """, """ & _
                Selection.Address() & """'"
    End Sub
    

    If SelectCell is in a sheet code module, then you need to include the sheet code name:

    Sub Assign()
        ActiveSheet.Shapes(1).OnAction = "'Sheet1.SelectCell """ & _
                Selection.Parent.Name & """, """ & _
                Selection.Address() & """'"
    End Sub
    
    0 讨论(0)
  • 2021-01-22 02:15

    If I'm reading this right, "SelectCell" is a macro name, and you're passing in "Sheet 1","$C$10" as parameters (as strings). Whenever you make a call on the right side of an assignment operator (equals sign) the parameters need to be passed in with parenthases. for example,

    .OnAction = "SelectCell "Sheet 1","$C$10""
    

    is wrong, and

    .OnAction = "SelectCell ("Sheet 1","$C$10")"
    

    is right.

    Try that, but there's not much I can go off without much code. You could also try to use the fully qualified macro name, in case the Macro is in another module.

    0 讨论(0)
提交回复
热议问题