+ Reply to Thread
Results 1 to 4 of 4

Find text in column, copy next row and paste in new sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    10-09-2009
    Location
    Nottinghamshire, England
    MS-Off Ver
    Excel 2007
    Posts
    2

    Find text in column, copy next row and paste in new sheet

    Hi everyone, would appreciate all the help you can give to create this macro.

    Essentially, I have 2 Sheets:
    "SheetWSS" = data to be copied
    "SheetWSD" = destination of copied data.

    Below is a sample of the data in "SheetWSS"
    ----------A------------------------B-------------------------C
    1------Trade ID -------Description---------System no.
    2--------579----------------Loan ---------------------- 7
    3--------580----------------Deposit---------------------22
    4--------702----------------Deposit--------------------- 11
    5--------703----------------Loan ----------------------- 58
    6--------732 ---------------Loan------------------------66
    7--------733----------------Deposit-------------------- 99
    etc...(no more than 10000)

    Now, an explanation of the data:
    1) I work for a small bank (CORP) that takes deposits and gives loan.
    2) CORP books these trades using only system no. 7 and 11; other system nos. belong to customers.
    3) When a single trade is booked, the 2 sides of the transaction is recorded (by Trade IDs) . E.g rows 2 and 3 relates to one trade. So if CORP loans money with system no.7 to CUSTB, who uses system no.22, it shows for CORP a loan and for CUSTB, a deposit.
    4) Everyday, the data in "SheetWSS" is updated with a different number of rows from previous, but the number of columns remain the same.

    So, here's what i'ld like the macro to do:
    1) Go into Column C, find system no.7 and 11.
    2) For each 7 and 11 found, Copy the next row . So if for e.g, system no.11 was found in C4, i want the ENTIRE ROW 5 to be copied.
    3) Paste the entire next row in "SheetWSD" until we have a list of all opposing sides of the same transactions initiated by CORP.

    Thanks in advance for your help.

    Regards,
    MikeTiplady.

  2. #2
    Registered User
    Join Date
    09-15-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Find text in column, copy next row and paste in new sheet

    This should do it:

    Sub CopyNextRow()
        
    k = Sheets("SheetWSD").Range("A65000").End(xlUp).Row + 1 'find last row in destination sheet
        
    For i = 2 To [a65000].End(xlUp).Row
        Select Case Cells(i, 4)
            Case 7, 11
                Sheets("SheetWSS").Rows(i + 1).Copy Destination:=Sheets("SheetWSD").Cells(k, 1) 'copy next row and past in destination sheet
                k = k + 1 'increase count for next row in destination sheet
        End Select
    Next i 'loop until last row in source sheet
    
    End Sub
    Last edited by roli001; 10-09-2009 at 09:03 PM.

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Find text in column, copy next row and paste in new sheet

    Hello MikeTiplady,

    Welcome to the Forum!

    This version is longer than the previous post, but is optimized for speed. Since you have about 10000 rows data, speed becomes important. The macro will also clear the destination sheet each time the macro runs. Both the number of rows and columns used are calculated by the macro so only cells of interest are copied.
    Sub Copy711()
    
      Dim Data() As Variant
      Dim DstRng As Range
      Dim LastCol As Long
      Dim I As Long
      Dim R As Long
      Dim RngEnd As Range
      Dim SrcRng As Range
      
        Set SrcRng = Worksheets("SheetWSS").Range("C1")
        Set DstRng = Worksheets("SheetWSD").Range("A1")
     
          LastCol = SrcRng.Parent.UsedRange.Cells(1, Columns.Count).End(xlToLeft).Column
          Set RngEnd = SrcRng.Parent.Cells(Rows.Count, SrcRng.Column).End(xlUp)
          Set SrcRng = SrcRng.Resize(RngEnd.Row + SrcRng.Row - 1, LastCol)
    
          ReDim Data(1 To SrcRng.Rows.Count, 1 To 1)
          Data = SrcRng.Columns(1).Value
          DstRng.Parent.UsedRange.ClearContents
          
            For I = 1 To UBound(Data, 1)
              If Data(I, 1) = 7 Or Data(I, 1) = 11 Then
                 SrcRng.Cells(I + 1, 1).EntireRow.Copy DstRng.Offset(R, 0)
                 R = R + 1
              End If
            Next I
            
    End Sub
    Adding the Macro
    1. Copy the macro above pressing the keys CTRL+C
    2. Open your workbook
    3. Press the keys ALT+F11 to open the Visual Basic Editor
    4. Press the keys ALT+I to activate the Insert menu
    5. Press M to insert a Standard Module
    6. Paste the code by pressing the keys CTRL+V
    7. Make any custom changes to the macro if needed at this time.
    8. Save the Macro by pressing the keys CTRL+S
    9. Press the keys ALT+Q to exit the Editor, and return to Excel.

    To Run the Macro...
    To run the macro from Excel, open the workbook, and press ALT+F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    10-09-2009
    Location
    Nottinghamshire, England
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Find text in column, copy next row and paste in new sheet

    Thank you very much roli001 and Leith Ross.

    Unfortunately, roli001's macro did not work. Cannot figure out why. Leith Ross's though, was great.

    This has greatly reduced my stress.

    Love the old Scottish proverb!

    Regards,
    Mike.Tiplady

+ 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