+ Reply to Thread
Results 1 to 1 of 1

Nested Loop problem/Algorithm

Hybrid View

  1. #1
    Registered User
    Join Date
    02-09-2012
    Location
    Calgary,Alberta
    MS-Off Ver
    Excel 2010
    Posts
    1

    Lightbulb Nested Loop problem/Algorithm

    I am comparing two columns from different sheets in the same workbook. If a match is found, I want to copy the data and paste it to a different sheet in the same workbook. But there are certain conditions that I have to account for when I perform copying and pasting


    Please open my workbook in attachments and have a look at it


    1)I want to first go to sheet1, pick an entry from columnB and then compare that entry with the entries of column B in the connections Sheet.

    2)When it hits the First match, I want to copy the matched row and copy all rows till string "Name" appears in column A of the connection Sheet

    3)Paste the copied data on to the sheet 2

    4)Now I want to check if the value of the cell in column B of sheet connection contains a string which is present in the current entry of column B of sheet 1

    5)if it does then copy everything till next Name in column A(this step continues until condition 6 gets true)

    6)If not then the go back to Step 1


    I am not a computer programmer, but I have tried writing a code after reading posts on the forum. I am anxiously looking for all of your replies


    
    
    Sub SearchForString()
    
        Dim i As Integer
        Dim k As Integer
        Dim j As Integer
        
        i = 1
    
        k = 1
         
        j = 1
        
        Sheets("Sheet1").Select
        
    
    
    
    'For j = 1 To ActiveSheet.Range("B").Value (This doesnt work :( )
    
    
    
    Do While Sheets("connections").Range("B" & CStr(i)).Value = Sheets("Sheet1").Range("B" & CStr(j)).Value Or InStr(Sheets("connections").Range("B" & CStr(i)).Value, Sheets("Sheet1").Range("B" & CStr(j)).Value)
      
       Sheets("connections").Select
            
            Rows(CStr(i) & ":" & CStr(i)).Select
                
            Selection.Copy
                
                'Paste row into Sheet1 in next row
       
               Sheets("Sheet2").Select
           
                Rows(CStr(k) & ":" & CStr(k)).Select
                ActiveSheet.Paste
                
               ' Move counter to next row
        k = k + 1
        i = i + 1
        Sheets("connections").Select
    
    Loop
    
    
    Do
        Sheets("connections").Select
            
            Rows(CStr(i) & ":" & CStr(i)).Select
                
            Selection.Copy
               Sheets("Sheet2").Select
           
                Rows(CStr(k) & ":" & CStr(k)).Select
                ActiveSheet.Paste
                
            
        k = k + 1
        i = i + 1
        Sheets("connections").Select
    
     
     Loop Until InStr(Sheets("connections").Range("A" & CStr(i)).Value, "Name")
    
    
    
    MsgBox j
    
    
    
    
    
    'Next
    
    
    
    End Sub
    Attached Files Attached Files
    Last edited by Asad74; 02-11-2012 at 09:38 PM. Reason: I forgot to attach my code

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1