如何过滤列中的每个名称并为每个过滤后的名称创建电子邮件?

时间:2019-03-08 16:12:56

标签: excel vba

所以我有此代码,该代码在列中过滤1个名称,并将一封电子邮件及其数据发送给过滤后的用户。我该如何做一个for next循环,以过滤A列中的其他名称,从而在发送电子邮件时也如此?

Sub Button1_Click()

Dim Outlook As Object
Dim Email As Object
Dim opmail As Object
Dim page As Object
Dim outApp As Object
Dim outRec As Object
Dim outAL As Object
Dim outTI As Object
Dim newSh As Worksheet
'Dim recName As String
Dim rng As Range

myFile = Application.GetOpenFilename(, , "Browse for Workbook")
If myFile = False Then Exit Sub
Set wb = Workbooks.Open(myFile)
'Set wb = ActiveSheet

wb.ActiveSheet.Range("callbackqueue[#ALL]").AutoFilter Field:=1, 
Criteria1:=Array("CALMA"), Operator:=xlAnd

'Next

 Set outApp = CreateObject("Outlook.Application")
 Set outAL = outApp.Session.AddressLists.Item("Global Address List")
 Set outTI = outApp.CreateItem(0)


bankSID = InputBox("Enter SID")
Set outRec = outTI.Recipients.Add(bankSID)
outRec.Resolve
recName = outRec.AddressEntry.Name

'With Email
With outTI
    .To = bankSID
    .Subject = "Subject Line"
    .Body = "See assigned information below" & vbCrLf & "Regards"
    .Display

    'Set opmail = Email.GetInspector
    Set outAL = outTI.GetInspector
    Set page = outAL.WordEditor


    ActiveSheet.Range("A6:E16").Copy

    page.Application.Selection.Start = Len(.Body)
    page.Application.Selection.End = page.Application.Selection.Start
    page.Application.Selection.PasteandFormat (wdFormatPlainText)
    .Display
    .Send
    Set page = Nothing
    Set opmail = Nothing





End With

Set Email = Nothing
Set Outlook = Nothing
End Sub

很抱歉这个问题仍然开始理解VB。

1 个答案:

答案 0 :(得分:0)

阿奎诺克(Aquinokj),如果您希望所有在指定范围的 callbackqueue中的人,则可以执行For Each循环。或者,您可以使用InputBox来分配值-很多方式,只需要更多信息...但是...

只是一个例子:

Option Explicit

Sub CopyStuff()

    Dim ws As Worksheet
    Dim cell As Range

    Set ws = ThisWorkbook.ActiveSheet

    For Each cell In ws.UsedRange.Columns(1).Cells
        wb.ActiveSheet.Range("callbackqueue[#ALL]").AutoFilter Field:=1, _
        Criteria1:=Array(cell.Value), Operator:=xlAnd

        'rest of code here

    Next

End Sub