Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在excel 2016的.onAction中使用Vba excel传递参数_Excel_Vba - Fatal编程技术网

在excel 2016的.onAction中使用Vba excel传递参数

在excel 2016的.onAction中使用Vba excel传递参数,excel,vba,Excel,Vba,我正试图通过onAction传递值,即sArg,但我不能这样做。 我试过这样做:“'btnT”“”&sArg&“”,但这不起作用 For i = 3 To LastRow Step 1 Set t2 = ActiveSheet.Range(Cells(i, LastCol + 3), Cells(i, LastCol + 3)) Set btn2 = ActiveSheet.Buttons.Add(t2.Left, t2.Top, t2.Width, t2.Height)

我正试图通过onAction传递值,即sArg,但我不能这样做。 我试过这样做:
“'btnT”“”&sArg&“”
,但这不起作用


For i = 3 To LastRow Step 1

    Set t2 = ActiveSheet.Range(Cells(i, LastCol + 3), Cells(i, LastCol + 3))
    Set btn2 = ActiveSheet.Buttons.Add(t2.Left, t2.Top, t2.Width, t2.Height)
    sArg = CStr(i)
    With btn2

         .OnAction = "Sheet1.btnT"
      .Caption = "View " & i
      .Name = CStr(i)
    End With
Next i

那么函数是

Sub btnT(Text)
MsgBox Text
Exit Sub


你只需要把单引号和双引号括起来

如果参数是数字(如1),则使用

如果参数是字符串,则使用

.OnAction = "'SubName ""SomeText""'"
子演示()
我想我会坚持多久
Dim-Arg作为变体
变暗t2as范围
调光btn2 As按钮
对于i=3到4,步骤1
设置t2=ActiveSheet.Range(单元格(i,LastCol+3),单元格(i,LastCol+3))
设置btn2=ActiveSheet.Buttons.Add(t2.Left,t2.Top,t2.Width,t2.Height)

Arg=i'将控件添加到工作表后,必须使用单独的函数捕获
单击事件。如果您不能(通常也不需要)将参数传递给宏,请参见。在您的情况下,您希望传递按钮名称,因此只需要在Sheet1.btnT过程中使用。当我尝试执行脚本2-3次时,调用者会给我类型不匹配错误
.OnAction = "'SubName ""SomeText""'"
Sub Demo()
    Dim i As Long
    Dim Arg As Variant
    Dim t2 As Range
    Dim btn2 As Button

    For i = 3 To 4 Step 1

        Set t2 = ActiveSheet.Range(Cells(i, LastCol + 3), Cells(i, LastCol + 3))
        Set btn2 = ActiveSheet.Buttons.Add(t2.Left, t2.Top, t2.Width, t2.Height)
        Arg = i '<~~ adjust to suit your needs
        With btn2
            .Caption = "View " & i
            .Name = CStr(i)
            If IsNumeric(Arg) Then
                .OnAction = "'Sheet1.btnT " & Arg & "'"
            Else
                .OnAction = "'Sheet1.btnT """ & Arg & """'"
            End If
        End With
    Next i
End Sub

Sub btnT(Arg As Variant)
    MsgBox Arg
End Sub