'How to create a loop that ignores hidden rows in VBA?

I am running a simple process that loops through a sheet and send an email based on Name, Email, Subject, CC and Country as shown in the image.

enter image description here

The image is only showing row 5 and 10 but the macro is running all rows from 2 to 10 and generates 9 email drafts instead of 2 email drafts.

May I know how to only run the loop on existing rows in the sheet?

Sub send_mass_email()
Dim i As Integer
Dim name, email, body, subject, copy, place, business As String
Dim OutApp As Object
Dim OutMail As Object
Dim cel As Range

body = ActiveSheet.TextBoxes("TextBox 1").Text

i = 2
'Loop down name column starting at row 2 column 1

Do While Cells(i, 1).Value <> ""
  
    name = Split(Cells(i, 1).Value, " ")(0) 'extract first name
    email = Cells(i, 2).Value
    subject = Cells(i, 3).Value
    copy = Cells(i, 4).Value
    business = Cells(i, 5).Value
    place = Cells(i, 6).Value
    
    'replace place holders
    body = Replace(body, "C1", name)
    body = Replace(body, "C5", business)
    body = Replace(body, "C6", place)
    
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    With OutMail
        .to = email
        .cc = copy
        .subject = subject
        .body = body
        '.Attachments.Add ("") 'You can add files here
        .display
        '.Send
    End With
    
    'reset body text
    body = ActiveSheet.TextBoxes("TextBox 1").Text
    
    i = i + 1
    
Loop

Set OutMail = Nothing
Set OutApp = Nothing
MsgBox "Email(s) Created!"

End Sub


Solution 1:[1]

Add an IF THEN in your Loop to check if the row is hidden or not. If true, do nothing:

Do While Cells(i, 1).Value <> ""

    If Cells(i, 1).EntireRow.Hidden = False Then
        'rest of your code
        '
        '
        '
        '
    End If

    i = i + 1
    
Loop

When filtering a range, those rows are hidden, so their property Hidden becomes True. If False it means those are the visible rows where you want to send email.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Foxfire And Burns And Burns