2015-10-21 32 views
0

下面的VBA會向多個用戶發送電子郵件,但只有第一個收件人才能收到正確的電子郵件。因此,如果A列中有10個地址,那麼地址1將會得到正確的電子郵件,其他所有人都會得到For這個詞,而不是別的。我不知道如何解決它,但認爲這是抵消?謝謝 :)。括號excel電子郵件VBA適用於一個收件人並非全部

正確的電子郵件是數據從

**For 10/21/2015** (Msg = "For " & c.Offset(, 1) & Chr(14) & Chr(14) 

**-There are no issues to report in the HLA & Molecular Diagnostics Laboratory.** ( For i = 3 To 14 
      If LCase(WS.Cells(c.Row, i)) = "x" Then 
       Msg = Msg & " -" & WS.Cells(1, i) & Chr(14) 
      End If 
     Next) 

VB

Private Sub CommandButton1_Click() 

Dim WS As Worksheet, Rng As Range, c As Range 
Dim OutApp As Object, OutMail As Object 
Dim Msg As String, Addr As String, FName As String, i As Long 
Dim obj As Object 
Dim MyFile As String, MyFileCopy As String 

'define path 
MyFileCopy = "L:\NGS\HLA LAB\total quality management\QC & QA\DOSE reports\DOSE reporting form Attachment.xlsx" 

'create a separate sheet2 to mail out 
Sheets(2).Copy 
Set wkb = ActiveWorkbook 
With wkb 
    .SaveAs MyFileCopy 
    .Close True 
End With 

' create connection, check condition, send email 
Set OutApp = CreateObject("Outlook.Application") 
Set WS = ThisWorkbook.Sheets("Email") 
With WS 
    Set Rng = .Range("A2", .Range("A" & .Rows.Count).End(xlUp)) 
End With 

For Each c In Rng 

     Msg = "For " & c.Offset(, 1) & Chr(14) & Chr(14) 
     For i = 3 To 14 
      If LCase(WS.Cells(c.Row, i)) = "x" Then 
       Msg = Msg & " -" & WS.Cells(1, i) & Chr(14) 
      End If 
     Next 

     Set OutMail = OutApp.CreateItem(0) 

     With OutMail 
      .To = c.Offset(, 0) 
      .CC = "" 
      .BCC = "" 
      .Subject = "Daily Operational Safety Briefing" 
      .Body = Msg 
      If LCase(c.Offset(, 3)) = "x" Then .Attachments.Add MyFileCopy, 1 
      .Send 
     End With 

Next c 

'confirm message sent and delete copy 
MsgBox "The data has been emailed sucessfully.", vbInformation 
Kill MyFileCopy 

Set OutMail = Nothing 
Set OutApp = Nothing 

' Exit and do not save 
Application.Quit 
ThisWorkbook.Close SaveChanges:=False 

End Sub 
+0

不知道,如果我得到你的權利,但是你的消息文本是否在每一行的B列中,還是僅在B2中? – therak

+0

「For」後面的日期在B2中,下一行的註釋來自C2或D2。數據總是來自這些單元格。所以即使有10個電子郵件地址B2,C2或D2也會被使用。取決於'x'決定使用的評論。謝謝 :)。 – Chris

回答

1

根據您的評論:So even if there are 10 email addresses B2 and either C2 or D2 will be used您需要稍微更改代碼。

當前您正在抓取與電子郵件地址相同的單元格。因此,如果電子郵件地址是A4,你要使用B4,C1,D1 ....

更改了代碼的一部分,這一點:

Msg = "For " & ws.Cells(2, 2) & Chr(14) & Chr(14) 
    For i = 3 To 14 
     If LCase(WS.Cells(c.Row, i)) = "x" Then 
      Msg = Msg & " -" & WS.Cells(2, i) & Chr(14) 
     End If 
    Next 
+0

非常感謝:)。 – Chris

0
.Range("A2", .Range("A" & .Rows.Count).End(xlUp) 

不應該這個是

.Range("A2", .Range("A2").End(xlDown).Address) 

然後

For Each c In Rng 

不應該這是Rng.Cells?

+2

兩者都有效。 OP的獲取A列中使用的單元格區域的方法起作用,而不是一直向下,直到它找到一個空單元格,它從底部開始並出現,直到找到填滿的單元格。第二,範圍的默認值是「cells」,不需要說明。 –

相關問題