从Excel列表生成自动语句到多个收件人

时间:2016-02-16 15:27:20

标签: excel-vba automation excel-formula vba excel

当我的同事参加将在国外举行的会议或会议时,他们有权获得小额津贴。每个月计算每个津贴,并按如下方式在Excel上进行批量支付(图片) 在此列表中,我正在寻找一种方法来自动为列表中的每个工作人员创建单个语句(包含on或多个事务)。

enter image description here 有谁知道我应该从哪里开始,我可以自动生成一堆语句,通过电子邮件发送给列表中的每个员工?

该陈述应包含:

全名 PO 排序代码 帐号 金额以欧元计算 金额为£ 汇率 用户参考 可能是底部的一两句话(付款条款和使用的政策)

提前感谢您的帮助。 ABS

1 个答案:

答案 0 :(得分:1)

编辑:以下内容仅接受单个交易并通过电子邮件发送给用户。将完整语句组合在一起最好使用用户表单来完成,这样您就可以控制要包含的数据。我建议初学VBA课程(检查w3学校)熟悉基础知识,然后可以调整以下内容来做你想要的。

从Excel中点击Alt + F11,然后双击此工作表,然后粘贴以下代码:

Sub sendemails()

Application.DisplayAlerts = False

Dim i As Long
i = 7

Dim UserNameCol As String
UserNameCol = "U"

Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")

Do Until Len(Cells(i, UserNameCol).Value) = 0 'will stop when blank cell appears

'Set up all the variables you need for your emails and fill them
    Dim UserName As String
    Dim FullName As String
    Dim PO As String
    Dim SortCode As String
    Dim AccountNum As String
    Dim Euro As String
    Dim Pound As String
    Dim ExchRate As String

'i is the counter to move through the list and the username col is U
    UserName = Cells(i, UserNameCol).Value
    FullName = Cells(i, UserNameCol).Offset(, -19).Value
    SortCode = Cells(i, UserNameCol).Offset(, -16).Value
    AccountNum = Cells(i, UserNameCol).Offset(, -15).Value
    PO = Cells(i, UserNameCol).Offset(, -14).Value
    Euro = Cells(i, UserNameCol).Offset(, -13).Value
    Exchange = Cells(i, UserNameCol).Offset(, -12).Value
    Pound = Cells(i, UserNameCol).Offset(, -11).Value
    ExchRate = Cells(i, UserNameCol).Offset(, -10).Value

    Set OutMail = OutApp.CreateItem(0) 'this goes here because a new email is needed each time
    On Error Resume Next
    With OutMail

        'Customise the below as required
        .To = UserName
        .Importance = 1
        .Subject = "Hello"
        .Body = "Full Name: " & FullName & vbCr & _
                    "PO: " & PO & vbCr & _
                    "Sort Code: " & SortCode & vbCr & _
                    "Account Number: " & AccountNum & vbCr & _
                    "Amount Euro: " & Euro & vbCr & _
                    "Amount Pound: " & Pound & vbCr & _
                    "Exchange Rate: " & ExchRate & vbCr & vbCr & _
                    "Thanks for reading."

        'display shows each email before sending
        .Display
        'send sends email automatically
        '.Send

    End With
    On Error GoTo 0

    i = i + 1

Loop

'destroy outlook when finished processing all mails
Set OutMail = Nothing
Set OutApp = Nothing

Application.DisplayAlerts = True

End Sub

我会尝试保留.Display属性设置的一对夫妇,一旦你感到高兴它就行了,注释掉.Display并取消注释.Send(添加或删除在必要时在行的开头撇号。

还要确保启用了Microsoft Outlook对象库(工具>参考> MS Outlook对象库)。

希望这有帮助。