Just started experimenting with VBA today. Creating Excel sheet to track SOLD, PENDING, LOST that will allow salesmen to click a button to send a group email to one category at a time. After much searching I found some code that works well to send group email by checking a column to make sure a proper address is there. I found some other code that I thought would check the "Job Status" column so that only the "SOLD" or whatever would be chosen for email. I am a clueless beginner and need help. Here is the code that was working until I added the - If Sh.Cells(Target.Row, 7) = "PENDING" Then - part. Any help would be greatly appreciated. Thanks!
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
Please help!
of course I would prefer to send one mail per Person (if the recipients shall not know each other), but let's stay with your Problem.
you only need minor changes:
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 & ";"
I am not sure if you Need the part "If cell.Value Like "? @? .?*" " or if that is from copy-paste from the code you found... If you Need it, the last line would have to be replaced by
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)
the rest as you have it.
the Problem was caused by
If Sh.Cells(Target.Row, 7) = "PENDING" Then
as you have no definition for "sh" - but you also don't need it.
I hope this helps
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.