2017-04-18 36 views
0

我想問一下如何從列到行更改Ron de Bruins代碼(例如,行1包含名稱;行2的電子郵件;行3反映了是或否)。給一個範圍(行)中的每個人發郵件

Sub Test1() 
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm 
'Working in Office 2000-2016 
Dim OutApp As Object 
Dim OutMail As Object 
Dim cell As Range 

Application.ScreenUpdating = False 
Set OutApp = CreateObject("Outlook.Application") 

On Error GoTo cleanup 
For Each cell In Columns("B").Cells.SpecialCells(xlCellTypeConstants) 
    If cell.Value Like "?*@?*.?*" And _ 
     LCase(Cells(cell.Row, "C").Value) = "yes" Then 

     Set OutMail = OutApp.CreateItem(0) 
     On Error Resume Next 
     With OutMail 
      .To = cell.Value 
      .Subject = "Reminder" 
      .Body = "Dear " & Cells(cell.Row, "A").Value _ 
        & vbNewLine & vbNewLine & _ 
        "Please contact us to discuss bringing " & _ 
        "your account up to date" 
      'You can add files also like this 
      '.Attachments.Add ("C:\test.txt") 
      .Send 'Or use Display 
     End With 
     On Error GoTo 0 
     Set OutMail = Nothing 
    End If 
Next cell 
cleanup: 
Set OutApp = Nothing 
Application.ScreenUpdating = True 
End Sub 

非常感謝你提前!

回答

0

也許是這樣的...

Sub Test1() 
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm 
'Working in Office 2000-2016 
Dim OutApp As Object 
Dim OutMail As Object 
Dim Rng As Range 

Application.ScreenUpdating = False 
Set OutApp = CreateObject("Outlook.Application") 

On Error GoTo cleanup 
For Each Rng In Columns("B").Cells.SpecialCells(xlCellTypeConstants).Areas 
    If Rng.cell(2).Value Like "?*@?*.?*" And _ 
     LCase(Rng.Cells(3).Value) = "yes" Then 

     Set OutMail = OutApp.CreateItem(0) 
     On Error Resume Next 
     With OutMail 
      .To = Rng.cell(2).Value 
      .Subject = "Reminder" 
      .Body = "Dear " & Rng.cell(1).Value _ 
        & vbNewLine & vbNewLine & _ 
        "Please contact us to discuss bringing " & _ 
        "your account up to date" 
      'You can add files also like this 
      '.Attachments.Add ("C:\test.txt") 
      .Send 'Or use Display 
     End With 
     On Error GoTo 0 
     Set OutMail = Nothing 
    End If 
Next Rng 
cleanup: 
Set OutApp = Nothing 
Application.ScreenUpdating = True 
End Sub 

上面的代碼假定說B1 =空白,B2 =名稱,B3 =電子郵件地址,B4 = YES/NO和B5 =空。您可能有不同的組記錄在B列

+0

謝謝您的回覆!假設第18行(H18:AE18)包含電子郵件地址(由第19行{name}&「@ yahoo.com」派生),並且「是」或空白符號位於第20行,這仍然適用,因爲我更改了列(「B」)改爲「H」? –

+0

代碼正在循環訪問SpecialCells(xlCellTypeConstants).Areas中的一組單元格。因此,如果數據符合我在代碼下面提到的標準,那麼它也適用於H列。 – sktneer

0

相同的順序如果你的意思是姓名和電子郵件是在1和2行,每列一個人,那麼這個修改應該做的:

For Each cell In Rows(2).Cells.SpecialCells(xlCellTypeConstants) 
    If cell.Value Like "?*@?*.?*" And LCase(cell.offset(1).Value) = "yes" Then 
     ' .... 
     .To = cell.Value 
     .Body = "Dear " & cell.offset(-1).Value 
     ' ... 
+0

假設我使用返回「是」或「」(空格)的公式,該公式是否會影響值=「是」?像,它會字面上認爲是「是」作爲價值?我已經嘗試過更改代碼,它沒有顯示任何錯誤,可能是因爲清理,但我仍然無法讓它工作。 –

+0

@MarkSy如果第三行中的「yes」是公式的結果,那麼沒關係,因爲我們正在使用'.Value'。然而,「是」應該清理額外的空間(修剪)。 –

+0

很酷。我認爲我們得到的代碼是正確的,但由於某種原因沒有工作。請介意,如果我把你的檔案寄給你?非常感謝你 –

相關問題