+ Reply to Thread
Results 1 to 5 of 5

Macros that Copy and Paste Whole Rows

Hybrid View

  1. #1
    Registered User
    Join Date
    05-28-2015
    Location
    Lakeland, FL
    MS-Off Ver
    Excel 13
    Posts
    66

    Macros that Copy and Paste Whole Rows

    Is there a way to write a Macro that will look at Sheet 1 and look up the value in D3 and see if it is in Columns F to O in Sheet 2 and if there are matches copying and pasting that whole row from sheet 2 onto sheet 1 at A15. After copying pasting all the ones that matched with D3 than starting with D4 and doing the same thing until D7?

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

    Re: Macros that Copy and Paste Whole Rows

    
    'Is there a way to write a Macro that will look at Sheet 1, D3 to D7
    'and see if there are matches in Columns F to O in Sheet 2,
    'and if there are matches then copy and paste the whole row to sheet 1 at A15.
    
    Sub test()
    
    LR = 15
    
    For Count = 3 To 7
        If Cells(Count, 4).Value = "" Then GoTo Skip
        strValueToPick = "SELECT"
        With Sheets("Sheet2").Range("F:O")
            Set rngFind = .Find(Cells(Count, 4).Value, .Cells(1, 1), LookIn:=xlValues, lookat:=xlWhole)
            If Not rngFind Is Nothing Then
                strFirstAddress = rngFind.Address
                Set rngPicked = rngFind
                Do
                    Set rngPicked = Union(rngPicked, rngFind)
                    Set rngFind = .FindNext(rngFind)
                Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstAddress
            End If
        End With
        
        If Not rngPicked Is Nothing Then
        rngPicked.EntireRow.Copy Sheets("Sheet1").Cells(LR, 1)
        End If
        
    LR = Cells(Rows.Count, 1).End(xlUp).Row + 1
    
    Skip:
    
    Next
    
    End Sub
    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.

  3. #3
    Registered User
    Join Date
    05-28-2015
    Location
    Lakeland, FL
    MS-Off Ver
    Excel 13
    Posts
    66

    Re: Macros that Copy and Paste Whole Rows

    Thank you so much! Is there a way to add something in the Macro that will create a Pop Up saying "No Requests for this Seat" if it does not find anything in Sheet 2 that matches it?

  4. #4
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,310

    Re: Macros that Copy and Paste Whole Rows

    Possibly...
    Sub Copy_and_Paste_Whole_Rows()
        Dim vData As Variant
        Dim vFind As Variant
        Dim a As Integer
        Dim b As Long
        Dim c As Integer
        Dim StartRow As Long
        Dim rData As Range
        
        Application.ScreenUpdating = False
        StartRow = 15
        With Worksheets("Sheet2")
            Set rData = Application.Intersect(.Range("F:O"), .UsedRange)
            vData = rData
            vFind = Worksheets("Sheet1").Range("D3:D7")
            For a = LBound(vFind) To UBound(vFind)
                For b = LBound(vData, 1) To UBound(vData, 1)
                    For c = LBound(vData, 2) To UBound(vData, 2)
                        If vFind(a, 1) = vData(b, c) Then
                            .Rows(b).Copy Worksheets("Sheet1").Rows(StartRow)
                            StartRow = StartRow + 1
                        End If
                    Next c
                Next b
            Next a
        End With
        Application.ScreenUpdating = True
    End Sub

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

    Re: Macros that Copy and Paste Whole Rows

    Your ammended request is not very clear,

    I assume that the seat number you refer to is in Cells D3 to D7

    
    'Is there a way to write a Macro that will look at Sheet 1, D3 to D7
    'and see if there are matches in Columns F to O in Sheet 2,
    'and if there are matches then copy and paste the whole row to sheet 1 at A15.
    'If there are no matches then Messagebox:  "No Requests for this Seat"
    
    Sub test()
    
    LR = 15
    
    For Count = 3 To 7
        If Cells(Count, 4).Value = "" Then GoTo Skip
        strValueToPick = "SELECT"
        With Sheets("Sheet2").Range("F:O")
            Set rngFind = .Find(Cells(Count, 4).Value, .Cells(1, 1), LookIn:=xlValues, lookat:=xlWhole)
            If Not rngFind Is Nothing Then
                strFirstAddress = rngFind.Address
                Set rngPicked = rngFind
                Do
                    Set rngPicked = Union(rngPicked, rngFind)
                    Set rngFind = .FindNext(rngFind)
                Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstAddress
            End If
        End With
        
        If Not rngPicked Is Nothing Then
        rngPicked.EntireRow.Copy Sheets("Sheet1").Cells(LR, 1)
    
        Else
    
        MsgBox "No Requests For Seat:  " & Cells(Count,4).value , vbOKOnly, "No Requests"
    
        End If
        
    LR = Cells(Rows.Count, 1).End(xlUp).Row + 1
    
    Skip:
    
    Next
    
    End Sub

+ 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. [SOLVED] Copy & Paste Macros with too many variables for me
    By backyardfun in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-15-2013, 06:50 PM
  2. copy and paste with macros
    By selfhouse in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-13-2013, 10:06 PM
  3. [SOLVED] Copy and paste macros???
    By Marz73 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-31-2012, 10:00 AM
  4. [SOLVED] How to copy & paste using Macros
    By Paul1992 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-16-2012, 09:22 AM
  5. Seach, Copy, select paste location, paste using macros
    By helpdave in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-16-2010, 11:36 PM
  6. Copy and paste using Macros
    By Melyssa18 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-03-2010, 07:36 PM
  7. Copy and Paste Macros
    By dickives in forum Excel General
    Replies: 0
    Last Post: 01-20-2006, 04:49 PM
  8. Replies: 1
    Last Post: 01-04-2005, 06:06 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