looping through filtered results

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
721
Office Version
  1. 2016
Platform
  1. Windows
I found something to get me started, but can't see how to assign the visible cell values to strings. I'm probably not even explaining that correctly.

Here is the start from what I found:
VBA Code:
Sub test()

    Dim cl As Range, rng As Range
    Dim lr As Long
    Dim eto As String, ecc As String, esubj As String
    
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    Set rng = Range(Cells(2, 1), Cells(lr, 6))
    
    For Each cl In rng
        If cl.EntireRow.Hidden = False Then
            
        End If
    Next

End Sub

Here are a few filtered rows
Email.xlsb
ABCDEF
1GroupAreaMainFirst NameCell PhoneEmail
2Group11XRichardRichard@email.com
11Group31XJoshJosh@email.com
12Group31XRJRJ@email.com
Sheet3


I'll be using this to send emails, each unique Group will create one email. Based on the above, one email would be addressed to richard@email.com, and another email would be addressed to josh@email.com;rj@email.com.

I have created a few macros for sending emails, but this is the first attempt at using a filter.
What I can't figure out, is how to write a loop where the loop doesn't look at the hidden rows.

This is something that I created before having the idea of using a filter, it was a bit cumbersome.

VBA Code:
Sub in_work()

    Dim OutApp As Object
    Dim OutMail As Object
    Dim x As Long, i As Long
    Dim eto As String, ecc As String, esubj As String

    Set OutApp = CreateObject("Outlook.Application")
    
    lr = Cells(Rows.Count, 3).End(xlUp).Row
    
        For i = 11 To 23
            
            Set OutMail = OutApp.CreateItem(0)
            With OutMail
                
            If Cells(i, 2).Value = "x" Then
                    esubj = Cells(i, 1).Value
                    For x = 2 To lr
                        If Cells(x, 3).Value = esubj Then
                            If Cells(8, 2).Value = "x" Then
                                If Cells(2, 2).Value = "x" Then
                                    If Cells(x, 4).Value = "1" And Cells(x, 5).Value = "X" Then
                                        eto = Cells(x, 8).Value & ";" & eto
                                    End If
                                End If
                                If Cells(3, 2).Value = "x" Then
                                    If Cells(x, 4).Value = "2" And Cells(x, 5).Value = "X" Then
                                        eto = Cells(x, 8).Value & ";" & eto
                                    End If
                                End If
                                If Cells(4, 2).Value = "x" Then
                                    If Cells(x, 4).Value = "3" And Cells(x, 5).Value = "X" Then
                                        eto = Cells(x, 8).Value & ";" & eto
                                    End If
                                End If
                                If Cells(5, 2).Value = "x" Then
                                    If Cells(x, 4).Value = "4" And Cells(x, 5).Value = "X" Then
                                        eto = Cells(x, 8).Value & ";" & eto
                                    End If
                                End If
                                If Cells(6, 2).Value = "x" Then
                                    If Cells(x, 4).Value = "5" And Cells(x, 5).Value = "X" Then
                                        ecc = Cells(x, 8).Value & ";" & ecc
                                    End If
                                End If
                            
                            ElseIf Cells(8, 2).Value = "" Then
                                If Cells(2, 2).Value = "x" Then
                                    If Cells(x, 4).Value = "1" Then
                                        eto = Cells(x, 8).Value & ";" & eto
                                    End If
                                End If
                                If Cells(3, 2).Value = "x" Then
                                    If Cells(x, 4).Value = "2" Then
                                        eto = Cells(x, 8).Value & ";" & eto
                                    End If
                                End If
                                If Cells(4, 2).Value = "x" Then
                                    If Cells(x, 4).Value = "3" Then
                                        eto = Cells(x, 8).Value & ";" & eto
                                    End If
                                End If
                                If Cells(5, 2).Value = "x" Then
                                    If Cells(x, 4).Value = "4" Then
                                        eto = Cells(x, 8).Value & ";" & eto
                                    End If
                                End If
                                If Cells(6, 2).Value = "x" Then
                                    If Cells(x, 4).Value = "5" Then
                                        ecc = Cells(x, 8).Value & ";" & ecc
                                    End If
                                End If
                            End If
                        End If
                    Next x
                
                .To = eto
                .cc = ecc
                .Subject = esubj
                .Display
            
            End If
            
            End With
            
            eto = ""
            ecc = ""
            
        Next i
    
    Set OutMail = Nothing
    Set OutApp = Nothing
    
End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Could you attach again data WITHOUT filter and hightlight the email that should be sent, and clarify why that email?
 
Upvote 0
Hi,
Just a quick reply to your question.
SpecialCells(xlCellTypeVisible) method helps when you would like to use not hidden range in the filtered range(sample#1). Or you can also check it by if Row is hidden or not. (sample#2)

VBA Code:
Sub Sample1()
    For Each c In Intersect(Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible), Columns("C"))
        Debug.Print c.Address
    Next
End Sub

Sub Sample2()
    For i = 1 To Cells(Rows.Count, 3).End(xlUp).Row
        If Rows(i).Hidden = False Then
            Debug.Print Cells(i, "C").Address
        End If
    Next
End Sub
 
Upvote 0
Hi,
Just a quick reply to your question.
SpecialCells(xlCellTypeVisible) method helps when you would like to use not hidden range in the filtered range(sample#1). Or you can also check it by if Row is hidden or not. (sample#2)

VBA Code:
Sub Sample1()
    For Each c In Intersect(Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible), Columns("C"))
        Debug.Print c.Address
    Next
End Sub

Sub Sample2()
    For i = 1 To Cells(Rows.Count, 3).End(xlUp).Row
        If Rows(i).Hidden = False Then
            Debug.Print Cells(i, "C").Address
        End If
    Next
End Sub
Thank you, where I'm having difficulties is with unique values. Based on my filtered results, when I run the below code I get
Group1
Richard@email.com
Group3
Josh@email.com
Group3
RJ@email.com
VBA Code:
Sub test()

    Dim r As Range, rng As Range
    Dim lr As Long
    Dim eto As String, ecc As String, esubj As String
    
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    Set rng = Range(Cells(2, 1), Cells(lr, 6))
    
    For Each r In rng
        If r.EntireRow.Hidden = False Then
            
            If r = "Group1" Then
                esubj = r
                eto = r.Offset(, 5)
                
                Debug.Print esubj
                Debug.Print eto
            End If
            
            If r = "Group2" Then
                esubj = r
                eto = r.Offset(, 5)
                
                Debug.Print esubj
                Debug.Print eto
            End If

            If r = "Group3" Then
                esubj = r
                eto = r.Offset(, 5)
                
                Debug.Print esubj
                Debug.Print eto
            End If
        
        End If
        
    Next

End Sub
What I'm trying to get is:
Group1
Richard@email.com
Group3
Josh@email.com;RJ@email.com
 
Upvote 0
I got a lot closer. Based on the below, how could I write an array for Group1, Group2, Group3, Group4?

I was thinking that instead of duplicating everything, I could use an array and loop through the array.

With Group1, next

I'm unsure how to start.

VBA Code:
Sub test()

    Dim r As Range, rng As Range
    Dim lr As Long, rn As Long
    Dim eto As String, ecc As String, esubj As String
    
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    Set rng = Range(Cells(2, 1), Cells(lr, 1))
    
    For Each r In rng
        If r.EntireRow.Hidden = False Then
        rn = r.Row
            If Cells(rn, 1).Value = "Group1" And Cells(rn, 2).Value <> "5" Then
                eto = Cells(rn, 6).Value & ";" & eto
                esubj = Cells(rn, 1).Value
            ElseIf Cells(rn, 1).Value = "Group1" And Cells(rn, 2).Value = "5" Then
                ecc = Cells(rn, 6).Value & ";" & ecc
            End If
        End If
    Next
          
    Debug.Print esubj
    Debug.Print eto
    Debug.Print ecc
    
    esubj = ""
    eto = ""
    ecc = ""
    
    For Each r In rng
        If r.EntireRow.Hidden = False Then
        rn = r.Row
            If Cells(rn, 1).Value = "Group2" And Cells(rn, 2).Value <> "5" Then
                eto = Cells(rn, 6).Value & ";" & eto
                esubj = Cells(rn, 1).Value
            ElseIf Cells(rn, 1).Value = "Group2" And Cells(rn, 2).Value = "5" Then
                ecc = Cells(rn, 6).Value & ";" & ecc
            End If
        End If
    Next
          
    Debug.Print esubj
    Debug.Print eto
    Debug.Print ecc
    
    esubj = ""
    eto = ""
    ecc = ""

    For Each r In rng
        If r.EntireRow.Hidden = False Then
        rn = r.Row
            If Cells(rn, 1).Value = "Group3" And Cells(rn, 2).Value <> "5" Then
                eto = Cells(rn, 6).Value & ";" & eto
                esubj = Cells(rn, 1).Value
            ElseIf Cells(rn, 1).Value = "Group3" And Cells(rn, 2).Value = "5" Then
                ecc = Cells(rn, 6).Value & ";" & ecc
            End If
        End If
    Next
          
    Debug.Print esubj
    Debug.Print eto
    Debug.Print ecc
    
    esubj = ""
    eto = ""
    ecc = ""

    For Each r In rng
        If r.EntireRow.Hidden = False Then
        rn = r.Row
            If Cells(rn, 1).Value = "Group4" And Cells(rn, 2).Value <> "5" Then
                eto = Cells(rn, 6).Value & ";" & eto
                esubj = Cells(rn, 1).Value
            ElseIf Cells(rn, 1).Value = "Group4" And Cells(rn, 2).Value = "5" Then
                ecc = Cells(rn, 6).Value & ";" & ecc
            End If
        End If
    Next
          
    Debug.Print esubj
    Debug.Print eto
    Debug.Print ecc
    
    esubj = ""
    eto = ""
    ecc = ""

End Sub
Email.xlsb
ABCDEF
1GroupAreaMainFirst NameCell PhoneEmail
2Group11XRichardRichard@email.com
4Group13XBillyBilly@email.com
5Group13AmadouAmadou@email.com
6Group15XFerasFeras@email.com
11Group31XJoshJosh@email.com
12Group31XRJRJ@email.com
14Group33XBlakeBlake@email.com
16Group35XAndyAndy@email.com
Sheet3


The output is -

Group1
Amadou@email.com;Billy@email.com;Richard@email.com;
Feras@email.com;



Group3
Blake@email.com;RJ@email.com;Josh@email.com;
Andy@email.com;
 
Upvote 0
After a lot of trial and error, I sort of figured it out using some helper rows ;)

I never could figure out how to use an Array, but this works. I'll have to keep reading up.

The output is the same as above, only a lot fewer potential lines of code. (y)

VBA Code:
Sub Macro1()

    lr = Cells(Rows.Count, 1).End(xlUp).Row
    Range(Cells(2, 1), Cells(lr, 1)).SpecialCells(xlCellTypeVisible).Copy Cells(lr + 10, 1)
    Range(Cells(lr + 10, 1), Cells(lr + 10, 1).End(xlDown)).RemoveDuplicates Columns:=1
    
    lr2 = Cells(Rows.Count, 1).End(xlUp).Row
    
    For i = lr + 10 To lr2
    
        For x = 1 To lr
        
            If Cells(x, 1).Value = Cells(i, 1).Value And Rows(x).Hidden = False And Cells(x, 2) <> "5" Then
                esubj = Cells(i, 1).Value
                eto = Cells(x, 6).Value & ";" & eto
            ElseIf Cells(x, 1).Value = Cells(i, 1).Value And Rows(x).Hidden = False And Cells(x, 2) = "5" Then
                esubj = Cells(i, 1).Value
                ecc = Cells(x, 6).Value & ";" & ecc
            End If
            
        Next x
            
        Debug.Print esubj
        Debug.Print eto
        Debug.Print ecc
        
        esubj = ""
        eto = ""
        ecc = ""
    
    Next i
        
    Rows(lr + 10 & ":" & lr2).Delete
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,790
Messages
6,121,608
Members
449,038
Latest member
apwr

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