2017-04-18 3 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 =はい/いいえとB5 =ブランク。あなたが名前や電子メールが1行目と2行目にあることを意味する場合は、列B

+0

あなたの返信ありがとう! 18行目(H18:AE18)に電子メールアドレス(Row 19 {name}& "@ yahoo.com"から派生したもの)が含まれていて、 "yes"または空白記号が20行目にあるとします。 ( "B")を "H"にしますか? –

+0

コードは、SpecialCells(xlCellTypeConstants).Areas内のセルのグループを1つの列にループしています。したがって、データが下のコードで述べた基準を満たしている場合は、列Hでも同様に動作します。 – sktneer

0

で同じ順序で、レコードの各種設定を有していてもよく、列ごとに1人が、この変更は行う必要があります。

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

imが "yes"または ""(space)を返す数式を使用しているとします。この数式はvalue = "yes"に影響しますか?それは文字通り「はい」を価値と考えていますか?私はすでにコードを変更しようとしましたが、おそらくクリーンアップのために何らかのエラーが表示されませんでしたが、まだ動作させることができませんでした。 –

+0

@MarkSy 3行目の '' yes ''が式の結果である場合は、' .Value'を取っているので問題ではありません。しかし、 "yes"は余分なスペースをきれいにする必要があります(Trimmed)。 –

+0

Cool。私はコードが正しいと思っていますが、何らかの理由で働いていません。ファイルを送ってもらえますか?どうもありがとうございました –

関連する問題