是否可以“动态”将单元格值传递给VBA?

时间:2015-11-09 15:02:14

标签: excel excel-vba vba

我在Excel中有一个“表格”:

      A            B          C 
1   Value1      Susan       Button
2   Value2      Peter       Button
3   Value3      Mark        Button

我想点击“按钮”向Excel点击按钮的人发送电子邮件。

示例:

如果我点击第2行中的按钮,则发送电子邮件给Peter,如:

Dear Peter,

 Value2  Value2  Value2  Value2 Value2 Value2 Value2 Value2

Your sincerely..

是否可以“动态”将Excel单元格值传递给VBA?

我想只写一个SUB到按钮并在每一行中使用,但我不知道如何根据按哪一个按钮来引用单元格。

对于电子邮件发送,我使用它,效果很好:

Sub Send_Email_Using_VBA()
    Dim Email_Subject, Email_Send_From, Email_Send_To, _
    Email_Cc, Email_Bcc, Email_Body As String
    Dim Mail_Object, Mail_Single As Variant
    Email_Subject = "Trying to send email using VBA"
    Email_Send_From = "test"
    Email_Send_To = "test"
    Email_Body = "You have successfully sent an e-mail using VBA !!!!"
    On Error GoTo debugs
    Set Mail_Object = CreateObject("Outlook.Application")
    Set Mail_Single = Mail_Object.CreateItem(0)
    With Mail_Single
        .Subject = Email_Subject
        .To = Email_Send_To
        .cc = Email_Cc
        .BCC = Email_Bcc
        .Body = Email_Body
        .send
    End With
debugs:
    If Err.Description <> "" Then MsgBox Err.Description
End Sub

1 个答案:

答案 0 :(得分:1)

假设您使用的是标准表单对象而非ActiveX控件,则可以使用以下内容:

Dim rowNumber As Long

rowNumber = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row

这将返回按钮左上角所在单元格的行号。