"For Each" Loop - Skipping Rows

kmflynn

New Member
Joined
Jul 6, 2016
Messages
3
Hello,

I have a for each loop that is pulling email addresses to set as recipients of emails. Thing is, it pulls emails "for each" clientCode - some clientCodes have multiple email addresses and thus have multiple rows. These emails need to have all email addresses as recipients and right now this loop is only addressing these to the first email address and skipping over the rest (if there are more than one).

i.e. )
client code email address
A 1@ex.com
A 2@ex.com
B 1@ex.com
B 2@ex.com

I bolded the examples that this loop adds as recipients, as you can see it is skipping email addresses for which there are multiple contacts for one client code. Can someone help me edit this to make sure it pulls all email addresses for which a client code is the same? Below is a piece of the VBA script I have.

Code:
    For Each rw In ThisWorkbook.Sheets("Client Contact Info").ListObjects("ClientContacts").ListRows
           'findrows for client code
            If ThisWorkbook.Sheets("Client Contact Info").Range("A" & rw.Range.Row).Value = clientCode Then
            'SetRecipients
                Recipients = Recipients & ThisWorkbook.Sheets("Client Contact Info").Range("B" & rw.Range.Row).Value & ";"
            End If
    Next rw


Thanks to all in advance.

Best,

-Kev
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
If you are wanting the email address for each unique client here is what I did:

Sheet 1 Setup:
Excel Workbook
ABCDEFGH
1Client CodeEmail AddressUnique Client CodeClient CodeEmail Address
2A1 at ex.com
3A2 at ex.com
4B1 at ex.com
5B2 at ex.com
6
Sheet1


Here is after macro runs:
Excel Workbook
ABCDEFGH
1Client CodeEmail AddressUnique Client CodeClient CodeEmail Address
2A1 at ex.comAA1 at ex.com
3A2 at ex.comB2 at ex.com
4B1 at ex.comB1 at ex.com
5B2 at ex.com2 at ex.com
6
Sheet1


Here is the macro:
Code:
Sub Client_Email_Addresses()
Application.ScreenUpdating = False
Dim i As Long, mylastrow As Long
Dim myval As String, lastcolumn As Long
Dim add1 As Long, add2 As Long
Dim xrow As Long, lastrow As Long

Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row).Copy Range("D2")
Range("D2").Activate
Range(Selection, Selection.End(xlDown)).Select
Selection.RemoveDuplicates Columns:=Array(1), Header:=xlNo
Selection.Sort key1:=ActiveCell, order1:=xlAscending

mylastrow = Cells(Rows.Count, 4).End(xlUp).Row

For i = 2 To mylastrow

myval = Cells(i, 4).Value
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = _
    Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByColumns, _
        SearchDirection:=xlPrevious).Offset(0, -5).Column
add1 = Columns(1).Find(What:=myval, LookIn:=xlValues, LookAt:=xlWhole).Row
xrow = add1

Do

If Cells(xrow + 1, 1).Value <> myval Then
    add2 = xrow
    Exit Do
Else
    xrow = xrow + 1
End If

Loop Until xrow = lastrow + 1

Range(Cells(add1, lastcolumn), Cells(add2, lastcolumn)).Copy Cells(Rows.Count, 7).End(xlUp).Offset(1, 0)
Range(Cells(add1, 1), Cells(add1, 1)).Copy Cells(add1, 6)

Next

Range("C1").Select
Application.ScreenUpdating = True
End Sub
 
Upvote 0
I forgot to mention that I am assuming your client names are sorted in ascending order. If not you will need to sort them for the macro to work.
 
Upvote 0
This is the other way that I took your request:

Sheet 1 Setup:
Excel Workbook
ABCDEFGH
1Client CodeEmail AddressUnique Email AddressClient CodeEmail Address
2A1 at ex.com
3A2 at ex.com
4B1 at ex.com
5B2 at ex.com
6
Sheet1


After macro:
Excel Workbook
ABCDEFGH
1Client CodeEmail AddressUnique Email AddressClient CodeEmail Address
2A1 at ex.com1 at ex.comA1 at ex.com
3A2 at ex.com2 at ex.comB
4B1 at ex.comA2 at ex.com
5B2 at ex.comB
6
Sheet1


Macro:
Code:
Sub Client_Email_Addresses()
Application.ScreenUpdating = False
Dim i As Long, mylastrow As Long
Dim myval As String, lastcolumn As Long
Dim add1 As Long, add2 As Long
Dim xrow As Long, lastrow As Long

Range("B2:B" & Cells(Rows.Count, 1).End(xlUp).Row).Copy Range("D2")
Range("D2").Activate
Range(Selection, Selection.End(xlDown)).Select
Selection.RemoveDuplicates Columns:=Array(1), Header:=xlNo
Selection.Sort key1:=ActiveCell, order1:=xlAscending

Range(Range("A2"), Range("A2").End(xlToRight).End(xlDown)).Select
Selection.Sort key1:=Range("B2"), order1:=xlAscending, Header:=xlGuess
Range("C1").Select

mylastrow = Cells(Rows.Count, 4).End(xlUp).Row

For i = 2 To mylastrow

myval = Cells(i, 4).Value
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = _
    Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByColumns, _
        SearchDirection:=xlPrevious).Offset(0, -5).Column
add1 = Columns(2).Find(What:=myval, LookIn:=xlValues, LookAt:=xlWhole).Row
xrow = add1

Do

If Cells(xrow + 1, 2).Value <> myval Then
    add2 = xrow
    Exit Do
Else
    xrow = xrow + 1
End If

Loop Until xrow = lastrow + 1

Range(Cells(add1, lastcolumn - 1), Cells(add2, lastcolumn - 1)).Copy Cells(Rows.Count, 6).End(xlUp).Offset(1, 0)
Range(Cells(add1, lastcolumn), Cells(add1, lastcolumn)).Copy Cells(add1, 7)

Next

Range(Range("A2"), Range("A2").End(xlToRight).End(xlDown)).Select
Selection.Sort key1:=Range("A2"), order1:=xlAscending, Header:=xlGuess
Range("C1").Select
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks a ton for your responses.

I am about to take a crack at this with your first interpretation and the macro which is exactly what I am looking for.

Will follow up on this, thanks again.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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