+ Reply to Thread
Results 1 to 9 of 9

Loop Thru Rows - Copy / Paste Other Sheet

Hybrid View

  1. #1
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,021

    Loop Thru Rows - Copy / Paste Other Sheet

    .
    Ok ... my grey matter is having another meltdown here. I've spent too many hours on this already.

    Please help me with the CopyPaste macro so it will loop thru all rows in Col A searching for the requested term (a number), then copy/paste the specified cells from the found
    rows to the other sheet.

    Thank you.

    Sub cpypste()
    Dim rngC As Range
    Dim i As Integer
    Dim x As String
    Dim strLastRow As String
    Dim strToFind As String, FirstAddress As String
    Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("Main Data")
    Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Sheets("Group 2 Printout")
    
    x = InputBox("Enter Group Number (1 or 2): ", "Search Group ")
    
    
    With ws1.Range("A3:A100")
    strLastRow = Worksheets("Group 2 Printout").Range("A" & Rows.Count).End(xlUp).Row + 1
    
    For i = 2 To strLastRow
    
    strToFind = x
            Set rngC = .Find(what:=strToFind, LookAt:=xlPart)
            
                If Not rngC Is Nothing Then
                
                    FirstAddress = rngC.Address
                    
                        If ws1.Cells(i, 1) = strToFind Then
                   
                            ws1.Cells(i, 2).Copy ws2.Cells(i, 1)
                            ws1.Cells(i, 3).Copy ws2.Cells(i, 2)
                            ws1.Cells(i, 4).Copy ws2.Cells(i, 3)
                            ws1.Cells(i, 5).Copy ws2.Cells(i, 4)
                            ws1.Cells(i, 6).Copy ws2.Cells(i, 5)
                            ws1.Cells(i, 12).Copy ws2.Cells(i, 6)
                    
                        End If
                    
                    Set rngC = .FindNext(rngC)
                    
                End If
    
    Next i
    
    End With
    
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Loop Thru Rows - Copy / Paste Other Sheet

    Sub cpypste()
        
        Dim x         As Long
        Dim ws1       As Worksheet: Set ws1 = ThisWorkbook.Sheets("Main Data")
        Dim ws2       As Worksheet: Set ws2 = ThisWorkbook.Sheets("Group 2 Printout")
        
        Do
            DoEvents
            x = Application.InputBox("Enter Group Number (1 or 2): ", "Search Group ", Type:=1)
            If x = 0 Then Exit Sub    'User canceled
            If x <> 1 Or x <> 2 Then Exit Do Else MsgBox "Please enter a 1 or 2.", vbExclamation, "Invalid Entry"
        Loop
        
        If ws2.Range("A" & Rows.Count).End(xlUp).Row > 2 Then ws2.UsedRange.Offset(2).Clear
        
        If Not IsError(Application.Match(x, ws1.Range("A:A"), 0)) Then
        
            ws1.Range("A3").CurrentRegion.AutoFilter Field:=1, Criteria1:=x
            Intersect(ws1.AutoFilter.Range.Offset(1), ws1.Range("B:F,L:L")).Copy _
                Destination:=ws2.Range("A" & Rows.Count).End(xlUp).Offset(1)
            ws1.AutoFilterMode = False
            
        Else
            MsgBox "No match found for: " & x, vbExclamation, "Copy Canceled"
        End If
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,021

    Re: Loop Thru Rows - Copy / Paste Other Sheet

    .
    AlphaFrog

    Thank you for the assistance.

    I know you won't believe me but I was going to try your macro the very next thing ....

    ( ya .. right )


    Thank you. I DO appreciate the help.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Loop Thru Rows - Copy / Paste Other Sheet

    Quote Originally Posted by Logit View Post
    .
    AlphaFrog

    Thank you for the assistance.

    I know you won't believe me but I was going to try your macro the very next thing ....

    ( ya .. right )


    Thank you. I DO appreciate the help.
    You're welcome.

    Minor bug fix. Change this line...

            If x = 1 Or x = 2 Then Exit Do Else MsgBox "Please enter a 1 or 2.", vbExclamation, "Invalid Entry"

  5. #5
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Loop Thru Rows - Copy / Paste Other Sheet

    You omitted your Do Loop for the FindNext.
    After 'FirstAddress' line
    Do
        'your if statements and Set statement
    Loop While rngC.Address <> FirstAddress
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  6. #6
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Loop Thru Rows - Copy / Paste Other Sheet

    Is it what you need ?
    Sub cpypste1()
    Dim rngC As Range
    Dim i As Integer
    Dim DstLR As Integer
    Dim OrgLR  As Integer
    Dim x As String
    Dim strLastRow As String
    Dim strToFind As String, FirstAddress As String
    Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("Main Data")
    Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Sheets("Group 2 Printout")
    Const FR = 3
    
    
        x = InputBox("Enter Group Number (1 or 2): ", "Search Group ")
        With ws1
            OrgLR = .Cells(Rows.Count, "A").End(3).Row
            For i = FR To OrgLR
                If .Cells(i, 1) = x Then
                    DstLR = ws2.Range("A" & Rows.Count).End(xlUp).Row
                    .Cells(i, 2).Copy ws2.Cells(DstLR + 1, 1)
                    .Cells(i, 3).Copy ws2.Cells(DstLR + 1, 2)
                    .Cells(i, 4).Copy ws2.Cells(DstLR + 1, 3)
                    .Cells(i, 5).Copy ws2.Cells(DstLR + 1, 4)
                    .Cells(i, 6).Copy ws2.Cells(DstLR + 1, 5)
                    .Cells(i, 12).Copy ws2.Cells(DstLR + 1, 6): ws2.Cells(DstLR + 1, 6) = .Cells(i, 12)
                End If
            Next i
        End With
    End Sub
    Last edited by PCI; 07-24-2018 at 09:26 PM. Reason: Typo
    - Battle without fear gives no glory - Just try

  7. #7
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Loop Thru Rows - Copy / Paste Other Sheet

    
    Sub Test()
    
    Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("Main Data")
    Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Sheets("Group 2 Printout")
    
    strValueToPick = InputBox("Enter Group Number (1 or 2): ", "Search Group ")
    
    strLastRow = ws2.Range("A" & Rows.Count).End(xlUp).Row + 1
    
        With ws1.Range("A3:A100")
            Set rngFind = .Find(strValueToPick, .Cells(1, 1), LookIn:=xlValues, lookat:=xlWhole)
            If Not rngFind Is Nothing Then
                strFirstAddress = rngFind.Address
                Set rngpicked = rngFind.Offset(0, 1).Resize(, 5)
                Set rngpicked2 = rngFind.Offset(0, 11)
    
                Do
                    Set rngpicked = Union(rngpicked, rngFind.Offset(0, 1).Resize(, 5))
                    Set rngpicked2 = Union(rngpicked2, rngFind.Offset(0, 11))
                    
                    Set rngFind = .FindNext(rngFind)
                Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstAddress
            End If
        End With
        
        If rngpicked Is Nothing Then Exit Sub
    
       rngpicked.Copy Destination:=ws2.Cells(strLastRow, 1)
       rngpicked2.Copy Destination:=ws2.Cells(strLastRow, 6)
    
    End Sub
    Last edited by mehmetcik; 07-24-2018 at 09:29 PM.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  8. #8
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,158

    Re: Loop Thru Rows - Copy / Paste Other Sheet

    It's probably some expert symposium ?
    So, maybe also something with arrays to the collection ?
    Sub cpypste_1()
        With ThisWorkbook
            With .Sheets("Main Data")
                Dim x: x = InputBox("Enter Group Number (1 or 2): ", "Search Group ")
                If Trim(x) = "" Then Exit Sub: If Not IsNumeric(x) Then Exit Sub
                x = Abs(CInt(x))
                
                Dim tbltmp(), tbl(), ubR&, ubC&, i&, j&, k&: k = 0
                
                With .Range("a2").CurrentRegion
                    tbltmp = .Offset(2, 0).Resize(.Rows.Count - 2, .Columns.Count).Value
                End With
            End With
            
            ubR = UBound(tbltmp, 1): ubC = UBound(tbltmp, 2)
            
            For i = 1 To ubR
                If tbltmp(i, 1) = x Then
                    k = k + 1: ReDim Preserve tbl(1 To 6, 1 To k)
                    For j = 2 To 6
                        tbl(j - 1, k) = tbltmp(i, j)
                    Next
                    tbl(6, k) = tbltmp(i, 12)
                End If
            Next
            
            Erase tbltmp
            
            With .Sheets("Group 2 Printout")
                .Activate
                .Range("a" & .Rows.Count).End(xlUp).Offset(1, 0).Resize(UBound(tbl, 2), UBound(tbl, 1)).Value = Application.Transpose(tbl)
                Erase tbl
            End With
        End With
    End Sub

  9. #9
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,021

    Re: Loop Thru Rows - Copy / Paste Other Sheet

    .
    This is why I L O V E this Forum. So many good folks assisting !

    Thank you all !

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 3
    Last Post: 11-06-2014, 01:23 AM
  2. [SOLVED] How to write a code within a loop to copy and paste rows on to another Sheet
    By swade730 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-24-2014, 08:40 PM
  3. Help on how to Loop Copy Paste to End of the Table VBA/Macro that do two rows only
    By jomapac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-17-2013, 12:18 PM
  4. [SOLVED] Loop copy and paste in new sheet
    By tonnerre2000 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-18-2012, 12:22 AM
  5. Copy and Paste Rows For certain condition then loop
    By steve70070 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-14-2011, 06:05 PM
  6. Compare rows in column A and copy/paste if they are the same using a for next loop
    By Andrew C in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-07-2009, 01:26 PM
  7. Loop to copy rows and paste into worksheet
    By damien_carr1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-01-2009, 06:10 PM

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