Select the first filtered cell then move onto the next filtered cell down

后端 未结 3 1772
抹茶落季
抹茶落季 2021-01-12 15:37

I have an Excel spreadsheet that has contact details, for example:

    A                 B            C            D                    E
1   Select who you          


        
3条回答
  •  青春惊慌失措
    2021-01-12 16:25

    Try this code:

    Sub SendEmail()
        Dim objOutlook As Object
        Dim objMail As Object
        'Dim RowsCount As Integer
        'Dim Index As Integer
        Dim Recipients As String
        Dim Category As String
        Dim CellReference As Integer
        Dim RowLimit As String
        'New variables.
        Dim firstRow As Long
        Dim lastRow As Long
        Dim cell As Excel.Range
        Dim row As Long
    
    
    
        Set objOutlook = CreateObject("Outlook.Application")
        Set objMail = objOutlook.CreateItem(0)
    
    
        Category = Range("D1")
        If Category = "Email Address1" Then
            CellReference = 4
        ElseIf Category = "Email Address2" Then
            CellReference = 5
        End If
    
    
    
        With ActiveSheet
    
            'Find the first and last index of the visible range.
            firstRow = .AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).row
            lastRow = .Cells(.Rows.Count, 1).End(xlUp).row
    
    
            'Iterate through all the rows between [firstRow] and [lastRow] established before.
            'Some of those rows are hidden, but we will check it inside this loop.
            For row = firstRow To lastRow
    
                Set cell = .Cells(row, CellReference)
    
                'We are checking here if this row is hidden or visible.
                'Note that we cannot check the value of property Hidden of a single cell,
                'since it will generate Run-time error '1004' because a single cell cannot be
                'hidden/visible - only a whole row/column can be hidden/visible.
                'That is why we need to refer to its .EntireRow property first and after that we
                'can check its .Hidden property.
                If Not cell.EntireRow.Hidden Then
    
                    'If the row where [cell] is placed is not hidden, we append the value of [cell]
                    'to variable Recipients.
                    Recipients = Recipients & cell.Value & ";"
                End If
    
            Next row
    
        End With
    
    
        With objMail
            .To = Recipients
            .Subject = "This is the subject"
            .Display
        End With
    
        Set objOutlook = Nothing
        Set objMail = Nothing
    
    End Sub
    

提交回复
热议问题