1

I have the following piece of code which sends emails in bulk.

Sub Sengrd_Files()

    Dim OutApp As Object
    Dim OutMail As Object
    Dim sh As Worksheet
    Dim cell As Range
    Dim FileCell As Range
    Dim rng As Range
para2 = ""
para3 = ""

para232 = Range("AA2").Value

    With Application
        .EnableEvents = False
        .ScreenUpdating = True
    End With

    Set sh = Sheets("Sheet1")

    Set OutApp = CreateObject("Outlook.Application")

    For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)

        'Enter the path/file names in the C:Z column in each row
        Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")

        If cell.Value Like "?*@?*.?*" And _
           Application.WorksheetFunction.CountA(rng) > 0 Then
            Set OutMail = OutApp.CreateItem(0)

            With OutMail
                .to = cell.Value
                .Subject = "Circle Profitability Report for the period ended 30-NOV-2017"
                .Body = "Dear Sir/Madam," _
& vbNewLine _
    & para232 & vbNewLine _
    & vbNewLine & para2 & vbNewLine _
    & Remark & vbNewLine & vbNewLine _
    & para3 & vbNewLine & vbNewLine


                For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                    If Trim(FileCell) <> "" Then
                        If Dir(FileCell.Value) <> "" Then
                            .Attachments.Add FileCell.Value
                        End If
                    End If
                Next FileCell

                .Send  'Or use .Display
            End With

            Set OutMail = Nothing
        End If
    Next cell

    Set OutApp = Nothing
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub

enter image description here

7 different mails will be sent to different people mentioned in Column B with Attachment defined in Col C. The Macro by default sends mails for ALL line items probably because of this line in code

**For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)**

I cannot define a variable i and change the above line to

**For Each i =1 to 5 sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)** 

due to syntax error. Can anyone help me in syntax in replacing "For each cell in" to a finite range.

3
  • 2
    Why does your code look like it was hit by a truck? At the very least, delete the excessive line breaks. Commented Jan 22, 2018 at 10:08
  • why would you want to use i variable ? Commented Jan 22, 2018 at 10:10
  • What makes you think that line is the problem? Have you tried stepping through your code to debug? Commented Jan 22, 2018 at 10:11

1 Answer 1

2

This is how to make the bulk-mail-sender send only to a given range (in this case B2 - B5):

For Each cell In sh.Range("B2:B5")

And do not forget - spam is bad.

Sign up to request clarification or add additional context in comments.

5 Comments

@Michel - modify it without the .Cells.SpecialCells(xlCellTypeConstants) part.
when i change to to" For Each cell In sh.Range("B2:B5").Cells.SpecialCells(xlCellTypeConstants)" it limits to B5 ,but sends the 1st mail 4 items :(
@Michel - when you write only For Each cell In sh.Range("B2:B5") it will send it to B2, B3, B4 and B5. If you want to include B6 then write For Each cell in sh.Range("B2:B6"). And do not write the .Cells.SpecialCells(xlCellTypeConstants) part.
thanks a lot......should have avoided asking....i was so engaged in for i=1 to 5 for hours,that i almost forgot that i could have modified sh.Columns("B") in the next few words
@Michel - it is always good to ask, especially when you are giving your code & what you have tried so far.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.