简体   繁体   English

将具有多个参数的宏分配给按钮VBA

[英]Assign Macro with Multiple Arguments to Button VBA

I am working on a VBA macro which I would like to add buttons to each row. 我正在研究VBA宏,我想在每行中添加按钮。 I would like to assign the same macro to each button, with different arguments on each row. 我想为每个按钮分配相同的宏,每行具有不同的参数。 The macro the button runs is a simple one which generates an email from information on the row the button is on and saves it to the user's outlook drafts folder. 按钮运行的宏是一个简单的宏,它可以根据按钮所在的行上的信息生成电子邮件,并将其保存到用户的Outlook草稿文件夹中。

The issue I am having is that when I have the email code classified as a function, it immediately runs through instead of assigning it to the button. 我遇到的问题是,当我将电子邮件代码分类为功能时,它将立即运行,而不是将其分配给按钮。 When I have it classified as a sub, I receive a compile error which states 'Compile error: Expected Function or variable' 当我将其归类为子程序时,我收到一个编译错误,指出“编译错误:预期函数或变量”

The code for the main macro follows: 主宏的代码如下:

Sub addButtons()
    Dim lastCol As Integer
    Dim lastRow As Integer
    Dim r As Range
    Dim btn As Button
    Dim uid As String
    Dim rDate As String
    Dim i As Integer

    lastCol = Cells(1, Columns.Count).End(xlToLeft).Column + 1
    lastRow = Cells(Rows.Count, 1).End(xlUp).Row
    rDate = Str(Date)
    rDate = Replace(rDate, "/", "D")

    For i = 2 To lastRow
        Set r = ActiveSheet.Range(Cells(i, lastCol), Cells(i, lastCol))
        Set btn = ActiveSheet.Buttons.Add(r.Left, r.Top, r.Width, r.Height)
        uid = ActiveSheet.Cells(i, 1).Text
        With btn
            .OnAction = newhireEmail(i, rDate)
            .Caption = "Email " & uid & "?"
            .name = "btn" & (i - 1)
        End With
    Next i

    r.EntireColumn.ColumnWidth = 15
End Sub

I can post the code for the email as well, but I don't believe it should be relevant as I would ideally like that code to not be run at all unless the button is being clicked. 我也可以发布该电子邮件的代码,但是我不认为它应该与之相关,因为我希望该代码根本不运行,除非单击该按钮。

Thank you for your time! 感谢您的时间!

This may not be an answer to your question as posed, but I do want to present a couple of different ways of tackling this task. 这可能不是您所提出问题的答案,但我确实想提出几种解决该任务的方法。

One of the more lightweight ways of doing it is to subscribe to an event that the worksheet extends (and that you don't otherwise need). 一种更轻便的方法是订阅工作表扩展的事件(否则不需要该事件)。 For example, when I had a surprisingly similar process, I subscribed to the worksheet double click event. 例如,当我有一个惊人的相似过程时,我订阅了工作表双击事件。 When I Double Clicked on a cell, it compiled information from that row, and passed it to an email subroutine. 当我双击一个单元格时,它会编译该行中的信息,并将其传递给电子邮件子例程。

If you want a more visual clue, but you don't want the over head of a button, consider using Hyperlinks. 如果您希望获得更直观的线索,但又不想让按钮过于麻烦,请考虑使用超链接。 This is a two step process, but it when I've done this, it has worked more quickly and more efficiently than a ton of buttons. 这是一个两步过程,但是当我完成此过程时,它比大量按钮更快捷,更有效地工作。 (NOTE: you are limited to 65,530 hyperlinks per worksheet. If you need more than that, this won't work). (注意:每个工作表最多只能有65,530个超链接。如果您需要的链接数量更多,将无法正常工作)。

First, generate the Hyperlinks: 首先,生成超链接:

Sub AddHyperlinks()
    Dim lastCol As Long
    Dim lastRow As Long
    Dim s As Worksheet

    Set s = ActiveSheet


    lastCol = s.Cells(1, Columns.Count).End(xlToLeft).Column + 1
    lastRow = s.Cells(s.Rows.Count, 1).End(xlUp).Row

    For i = 2 To lastRow
        s.Hyperlinks.Add Anchor:=s.Cells(i, lastCol), _
        Address:="", _
        SubAddress:=s.Cells(i, lastCol).Address, _
        TextToDisplay:="Send Email"
    Next i
End Sub

Next, subscribe to the FollowHyperlinks event in the appropriate worksheet, and generate your code there. 接下来,订阅相应工作表中的FollowHyperlinks事件,并在此处生成代码。 The Hyperlink Target will have a reference to the cell address of the hyperlink, from which you can get any needed data: 超链接目标将引用超链接的单元格地址,从中可以获取任何需要的数据:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    'Submit whatever code you want here
    MsgBox Target.SubAddress
End Sub

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

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