[英]If a cell in range of cells in Excel contains an email address (@ sign), I want to move whole cell text into a different column
[英]VBA Excel - If cell in one column contains email address and cell in another column is “SOLD” , send email
今天才剛剛開始嘗試VBA。 創建Excel工作表以跟蹤“已售出”,“待處理”和“丟失”,這將使銷售人員可以單擊一個按鈕來一次將一組電子郵件發送到一個類別。 經過大量搜索后,我發現一些代碼可以很好地通過檢查一列以確保發送正確的地址來發送組電子郵件。 我找到了一些我認為會檢查“工作狀態”列的代碼,以便僅選擇“已售出”或任何其他形式的電子郵件。 我是一個笨拙的初學者,需要幫助。 這是直到我添加了-If Sh.Cells(Target.Row,7)=“ PENDING”然后-部分之前一直有效的代碼。 任何幫助將不勝感激。 謝謝!
Private Sub CommandButton1_Click()
Dim cell As Range
Dim strto As String
For Each cell In ThisWorkbook.Sheets("Sales 2013").Range("E3:E500")
If cell.Value Like "?*@?*.?*" Then
If Sh.Cells(Target.Row, 7) = "PENDING" Then
strto = strto & cell.Value & ";"
End If
End If
Next cell
If Len(strto) > 0 Then strto = Left(strto, Len(strto) - 1)
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
On Error GoTo cleanup
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "Anchor Sales"
.Bcc = strto
.Subject = "Enter subject here"
.Body = "" ' EMPTY FOR NOW
'USE THIS FOR ENTERING NAMES OF RECIPIENTS IN BODY TEXT "here"
'"Dear" & Cells(cell.Row, "A").Value _
& vbNewLine & vbNewLine & _
"Enter body text " & _
"here"
'You can add files also like this
'.Attachments.Add ("C:\test.txt")
'.Send 'Or use Display
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
Private Sub CommandButton2_Click()
End Sub
請幫忙!
當然,我希望每人發送一封郵件(如果收件人彼此之間不認識),但讓我們來解決您的問題。
您只需要進行少量更改:
Private Sub CommandButton1_Click()
Dim cell As Range
Dim strto As String
For Each cell In ThisWorkbook.Sheets("Sales 2013").Range("E3:E500")
If cell.Value = "PENDING" Then strto = strto & cell.offset(0, 1).value & ";"
我不確定是否需要“ If cell.Value Like”部分? @? 。?*“”還是來自您發現的代碼的復制粘貼...如果需要,則最后一行必須替換為
If cell.Value Like "?*@?*.?*" and cell.Value = "PENDING" Then strto = strto & cell.offset(0, 1).value & ";"
'in Offset(0,1) I assume the mailadress is in the cell next to the cell tested for "pending"
Next cell
If Len(strto) > 0 Then strto = Left(strto, Len(strto) - 1)
剩下的就隨你了。
問題是由
If Sh.Cells(Target.Row, 7) = "PENDING" Then
因為您沒有“ sh”的定義-但您也不需要它。
我希望這有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.