Stuck on logic for Excel problem...<p>Hi I am stuck on the logic for the following problem.<br><br>I have a list of employees and each has a manager attached.<span style="">  </span>I want to write a macro that will loop through all the employees on the sheet and compile a list of the managers with all employees reporting in to them.<span style="">   </span>I've attached an example as this may be hard to visualise!<br><br><a title="http://i102.photobucket.com/albums/m82/sc0ttb_2006/Sheet1.jpg" href="http://i102.photobucket.com/albums/m82/sc0ttb_2006/Sheet1.jpg">http://i102.photobucket.com/albums/m82/sc0ttb_2006/Sheet1.jpg</a></p> <p><br><a title="http://i102.photobucket.com/albums/m82/sc0ttb_2006/Sheet2.jpg" href="http://i102.photobucket.com/albums/m82/sc0ttb_2006/Sheet2.jpg">http://i102.photobucket.com/albums/m82/sc0ttb_2006/Sheet2.jpg</a></p> <p><br>I realise this will involve a for loop and then an IF statement or two and I know to use concatenate to add the names together into the one cell but I just can't get my head round the exact way to do it, so any help is greatly appreciated!<br><br>Many thanks</p>© 2009 Microsoft Corporation. All rights reserved.Wed, 10 Dec 2008 23:25:30 Z5ce3549d-3a46-40b6-be19-0fa7b550d4behttp://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/5ce3549d-3a46-40b6-be19-0fa7b550d4be#5ce3549d-3a46-40b6-be19-0fa7b550d4behttp://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/5ce3549d-3a46-40b6-be19-0fa7b550d4be#5ce3549d-3a46-40b6-be19-0fa7b550d4beScott Boydhttp://social.msdn.microsoft.com/Profile/en-US/?user=Scott%20BoydStuck on logic for Excel problem...<p>Hi I am stuck on the logic for the following problem.<br><br>I have a list of employees and each has a manager attached.<span style="">  </span>I want to write a macro that will loop through all the employees on the sheet and compile a list of the managers with all employees reporting in to them.<span style="">   </span>I've attached an example as this may be hard to visualise!<br><br><a title="http://i102.photobucket.com/albums/m82/sc0ttb_2006/Sheet1.jpg" href="http://i102.photobucket.com/albums/m82/sc0ttb_2006/Sheet1.jpg">http://i102.photobucket.com/albums/m82/sc0ttb_2006/Sheet1.jpg</a></p> <p><br><a title="http://i102.photobucket.com/albums/m82/sc0ttb_2006/Sheet2.jpg" href="http://i102.photobucket.com/albums/m82/sc0ttb_2006/Sheet2.jpg">http://i102.photobucket.com/albums/m82/sc0ttb_2006/Sheet2.jpg</a></p> <p><br>I realise this will involve a for loop and then an IF statement or two and I know to use concatenate to add the names together into the one cell but I just can't get my head round the exact way to do it, so any help is greatly appreciated!<br><br>Many thanks</p>Mon, 02 Apr 2007 08:01:10 Z2007-04-02T08:01:10Zhttp://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/5ce3549d-3a46-40b6-be19-0fa7b550d4be#50c2ae52-1c51-4921-9ddc-5c4a0d294c8ehttp://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/5ce3549d-3a46-40b6-be19-0fa7b550d4be#50c2ae52-1c51-4921-9ddc-5c4a0d294c8eSJOOhttp://social.msdn.microsoft.com/Profile/en-US/?user=SJOOStuck on logic for Excel problem...<p>hello </p> <p> </p> <p>only with your pics , i've just written a small macro.   </p> <p>i hope it be of help.</p> <p> </p> <p>best regards</p> <p> </p> <p>sjoo</p> <p> </p> <p>Sub compile_manager()<br>    Dim s1 As Worksheet<br>    Dim s2 As Worksheet<br>    <br>    Dim i As Long<br>    <br>    Dim strEmployee As String<br>    Dim strRecipient As String<br>    Dim varRecipients<br>    <br>    Dim rngEmp As Range<br>    Dim rngEmployees As Range<br>    Dim rngRec As Range<br>    Dim rngRecipients As Range<br>    <br>    Set s1 = ThisWorkbook.Worksheets(&quot;Sheet1&quot;)<br>    Set s2 = ThisWorkbook.Worksheets(&quot;Sheet2&quot;)<br>    <br>    <br>    Set rngEmployees = s1.Range(s1.Range(&quot;F2&quot;), s1.Range(&quot;F2&quot;).End(xlDown))<br>    Set rngRecipients = s2.Range(s2.Range(&quot;B2&quot;), s2.Range(&quot;B2&quot;).End(xlDown))<br>    <br>    For Each rngEmp In rngEmployees.Cells<br>        strEmployee = LCase(rngEmp.Value)<br>        <br>        For Each rngRec In rngRecipients.Cells<br>            <br>            '// split names with a seperator &quot;,&quot;<br>            varRecipients = Split(rngRec.Value, &quot;,&quot;)<br>            <br>            '// looping names<br>            For i = 0 To UBound(varRecipients)<br>                <br>                strRecipient = LCase(varRecipients(i))<br>                <br>                '// compare two names<br>                If strEmployee = strRecipient Then<br>                    <br>                    '// copy email &amp; first-last name to the sheet1<br>                    rngEmp.Offset(0, 1) = rngRec.Offset(0, 1) &amp; &quot; &quot; &amp; rngRec.Offset(0, 2)<br>                    rngEmp.Offset(0, -1) = rngRec.Offset(0, -1)<br>                    GoTo NEXT_REC<br>                End If<br>                <br>            Next<br>            <br>        Next</p> <p>NEXT_REC:</p> <p>    Next<br>End Sub<br></p>Mon, 02 Apr 2007 08:54:35 Z2007-04-02T08:54:35Zhttp://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/5ce3549d-3a46-40b6-be19-0fa7b550d4be#a85b7291-25a9-4f0d-ad2f-1803711e0531http://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/5ce3549d-3a46-40b6-be19-0fa7b550d4be#a85b7291-25a9-4f0d-ad2f-1803711e0531Scott Boydhttp://social.msdn.microsoft.com/Profile/en-US/?user=Scott%20BoydStuck on logic for Excel problem...Thank you so much for the code, i honestly didn't expect anyone to actually draft me out a full solution - not that im complaining!!<br><br>I have copied the code in and am trying to understand it so i can make any minor tweaks that are needed to get it working.  I have never used range before - i take it this works similar to a vlookup in this sense.<br><br>I am working my way through it but not sure what it does to the second sheet as it doesn't work 100% when i run it in its current state.  Does there have to be data in the second sheet or will it work if it was blank?Mon, 02 Apr 2007 15:18:25 Z2007-04-02T15:18:25Zhttp://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/5ce3549d-3a46-40b6-be19-0fa7b550d4be#f357a3f9-7b57-4102-806c-35a51d69be2dhttp://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/5ce3549d-3a46-40b6-be19-0fa7b550d4be#f357a3f9-7b57-4102-806c-35a51d69be2dSJOOhttp://social.msdn.microsoft.com/Profile/en-US/?user=SJOOStuck on logic for Excel problem...<p>yes, the code copies the data from the second sheet. but i didn't make the code check blank. </p> <p> </p> <p>if sheet1's F column(employees's name) &amp; sheet2's B column(recipients) have a blank cell, </p> <p>the range address is from the second row to row before blank cell.</p> <p>for example,</p> <p>B column has data from row 2 to row 10. but row 5 has a blank.</p> <p>the code, <font style="background-color:#ffff99">Set rngRecipients = s2.Range(s2.Range(&quot;B2&quot;), s2.Range(&quot;B2&quot;).End(xlDown)) </font>takes addresses from row2 to row4</p> <p>so the rngRecipients has &quot;B2:B4&quot; address. the other address(B5:B10) will be ignored. </p> <p> </p> <p>and i missed some codes that check a blank following comma in the recipients column.</p> <p>recipient column has names seperated with comma. </p> <p>you need to add the TRIM function to delete the blank.</p> <p>'-------------------------------------------------------------------</p> <p>    For Each rngEmp In rngEmployees.Cells<br>        strEmployee = LCase(<font style="background-color:#ffff00">Trim(rngEmp.Value</font>))<br>        <br>        For Each rngRec In rngRecipients.Cells<br>            <br>            '// split names with a seperator &quot;,&quot;<br>            varRecipients = Split(rngRec.Value, &quot;,&quot;)<br>            <br>            '// looping names<br>            For i = 0 To UBound(varRecipients)<br>                <br>                strRecipient = LCase(<font style="background-color:#ffff00">Trim(varRecipients(i))</font>)<br>                <br>                '// compare two names<br>                If strEmployee = strRecipient Then<br>                    <br>                    '// copy email &amp; first-last name to the sheet1<br>                    rngEmp.Offset(0, 1) = rngRec.Offset(0, 1) &amp; &quot; &quot; &amp; rngRec.Offset(0, 2)<br>                    rngEmp.Offset(0, -1) = rngRec.Offset(0, -1)<br>                    GoTo NEXT_REC<br>                End If<br>                <br>            Next<br>            <br>        Next</p> <p>NEXT_REC:</p> <p>    Next<br> '----------------------------</p> <p> </p> <p> </p>Mon, 02 Apr 2007 23:37:25 Z2007-04-02T23:37:25Zhttp://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/5ce3549d-3a46-40b6-be19-0fa7b550d4be#512355ea-c74c-434e-85db-b237039e6f9dhttp://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/5ce3549d-3a46-40b6-be19-0fa7b550d4be#512355ea-c74c-434e-85db-b237039e6f9dScott Boydhttp://social.msdn.microsoft.com/Profile/en-US/?user=Scott%20BoydStuck on logic for Excel problem...HI, thanks i understand it better now.  I think i need to explain better what i am trying to achieve here.  I basically start of with sheet 1 and have nothing in sheet 2.  I want to loop through all the rows of data in sheet 1 and from it, be able to produce something similar to what you see on sheet 2.  Really what i'm looking for is to have each manager listed with the employees that report to him.<br><br>I will have a go now and try to amend the code! <img height=19 alt=Smile src="http://forums.microsoft.com/MSDN/emoticons/emotion-1.gif" width=19>Tue, 03 Apr 2007 08:35:41 Z2007-04-03T08:35:41Zhttp://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/5ce3549d-3a46-40b6-be19-0fa7b550d4be#b35e5d54-9986-4c30-935d-ca62e6e67000http://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/5ce3549d-3a46-40b6-be19-0fa7b550d4be#b35e5d54-9986-4c30-935d-ca62e6e67000Scott Boydhttp://social.msdn.microsoft.com/Profile/en-US/?user=Scott%20BoydStuck on logic for Excel problem...<p>Ok so i am trying a slightly different approach here.  I've got the manager list working on the second sheet so simply need to use an IF statement to compare each row of data in the first sheet to the list of managers in the second one and if they match then copy the employee to the cell beside that manager.</p> <p> </p> <p> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p>Option Explicit</p> <p>Sub manager_list()</p> <p>Dim rngManager As Range<br>Dim rngList, c As Range<br>Dim x As Integer</p> <p>Dim Manager As String</p> <p>Dim s1, s2 As Worksheet</p> <p>Set s1 = ThisWorkbook.Worksheets(&quot;Sheet1&quot;)<br>Set s2 = ThisWorkbook.Worksheets(&quot;Sheet2&quot;)</p> <p>Set rngManager = s1.Range(s1.Range(&quot;E2&quot;), s1.Range(&quot;E2&quot;).End(xlDown))<br>Set rngList = s2.Range(s2.Range(&quot;A2&quot;), s2.Range(&quot;A2&quot;).End(xlDown))</p> <p>rngManager.AdvancedFilter xlFilterCopy, CopyToRange:=rngList, Unique:=True</p> <p>Manager = &quot;E&quot; &amp; x</p> <p>For x = 2 To 1000</p> <p>    For Each c In rngList.Cells</p> <p>        <font style="background-color:#ffff00">If Range(Manager).Value = c.Value Then<br></font>            MsgBox (&quot;TEST&quot;)<br>        Else<br>        'Insert code here to copy the employees name and concatenate it in the cell<br>        'next to the manager in sheet 2<br>        End If<br>    Next<br>Next</p> <p>End Sub</p></div></div> <p> </p> <p></p> <p> I just keep getting an error with the line above?  Can't figure it out!</p>Tue, 03 Apr 2007 13:11:16 Z2007-04-03T13:11:16Zhttp://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/5ce3549d-3a46-40b6-be19-0fa7b550d4be#eefa0be9-0f1d-4bc9-9faf-776bb48996f6http://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/5ce3549d-3a46-40b6-be19-0fa7b550d4be#eefa0be9-0f1d-4bc9-9faf-776bb48996f6Scott Boydhttp://social.msdn.microsoft.com/Profile/en-US/?user=Scott%20BoydStuck on logic for Excel problem...<p>Just an update to the thread as Sjoo kindley helped me via email.  Here is the current state:</p> <p> </p> <p> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p>Sub manager_list()</p> <p>Dim rngManager As Range<br>Dim rngList  As Range, c As Range<br>Dim x As Integer</p> <p>Dim Manager As String</p> <p>Dim s1, s2 As Worksheet</p> <p>Set s1 = ThisWorkbook.Worksheets(&quot;Sheet1&quot;)<br>Set s2 = ThisWorkbook.Worksheets(&quot;Sheet2&quot;)</p> <p>Set rngManager = s1.Range(s1.Range(&quot;E2&quot;), s1.Range(&quot;E2&quot;).End(xlDown))<br>Set rngList = s2.Range(s2.Range(&quot;A2&quot;), s2.Range(&quot;A2&quot;).End(xlDown))</p> <p>rngManager.AdvancedFilter xlFilterCopy, CopyToRange:=rngList, Unique:=True</p> <p>For x = 2 To 1000</p> <p>Manager = &quot;E&quot; &amp; x</p> <p>    For Each c In rngList.Cells<br>        <br>        If s1.Range(Manager).Value = c.Value Then<br>        <font style="background-color:#ffff00">'Insert code here to copy the employees name it in to the cell<br>        'next to the manager and concatenate any other names added under this manager</font><br>        Else<br>        'go to next record<br>        End If<br>    Next<br>    <br>Next<br>End Sub</p></div></div> <p> </p> <p></p> <p> I am now trying to work on the final part of the macro where i need to copy the employee's name to the cell next to the manager on sheet 2 and concatenate any others employees that may be under him into the same cell.</p> <p> </p> <p>Thanks <img height=19 alt=Smile src="http://forums.microsoft.com/MSDN/emoticons/emotion-1.gif" width=19></p>Wed, 04 Apr 2007 14:59:48 Z2007-04-04T14:59:48Z