Email visible entries only (filtered) with VBA

jenksdev

New Member
Joined
Sep 14, 2019
Messages
10
Hi all.

I am unable to attach files for some reason (it says I do not have permissions to) so, please bear with me.

I have created a sheet that looks like this: https://imgur.com/a/7pnr4M1

I have the following VBA doing the job:

Code:
Sub EmaiList()


    Dim OutApp As Object
    Dim OutMail As Object
    Set OutApp = CreateObject("Outlook.Application")
    Dim EmailList As Integer
    Dim strbody As String


    
    
With Application
    .ScreenUpdating = False
End With


' Define location of email list


EmailList = Application.Range("A1").CurrentRegion.Rows.Count


' Set definition of row


For r = 1 To EmailList


' Create body of email - note HTML


emailBody = "Dear " & Cells(r, 2) & ",<br><br>" & _
            "How are you?<br><br>" & _
            "Kind regards."
            
' Create the email


    Set OutMail = OutApp.CreateItem(0)


' Create email content


    With OutMail
        .To = Cells(r, 1).Value
        .Subject = "Tax reminder"
        .HTMLBody = emailBody & "<br>" & .HTMLBody
        .Save
    End With
    
' Action next row after opening previous email


Next r


' When no more rows, end query


Set OutMail = Nothing
Set OutApp = Nothing


End Sub

There are two problems:

1. An error occurs on line one, as it tries to send an email to the row titled 'Email'. How can I skip this row?

2. It also includes the filtered address (note the filter is on, hiding row three). How can I have it only include the visible rows?

Any help is much appreciated.

Kind regards,

Ben
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
How about
Code:
For R = 2 To EmailList
    If Rows(R).Hidden = False Then
        
        ' Create body of email - note HTML
        
        
        emailBody = "Dear " & Cells(R, 2) & "," & _
                    "How are you?" & _
                    "Kind regards."
                    
        ' Create the email
        
        
            Set OutMail = OutApp.CreateItem(0)
        
        
        ' Create email content
        
        
            With OutMail
                .To = Cells(R, 1).Value
                .Subject = "Tax reminder"
                .HTMLBody = emailBody & "" & .HTMLBody
                .Save
            End With
            
        ' Action next row after opening previous email
    End If

Next R
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top