Click here to Skip to main content
15,899,634 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i would like to ask.

My situation is
Sheet 1 includes value from Column A to Z.
Sheet 5 includes value from Column A to Z.

Both sheet, Column Z has common value.

I would like to use a VBA code that run to
1. match Column Z of (Sheet 5) to Column Z of Sheet 1.
2. If match found, then copy, paste the row from Sheet 5(*that match), to Sheet 1 respective row.

Please help!

What I have tried:

With .Range("Z1:Z" & LastRow)
.Autofilter Field:=1, Criteria1:="=*" & strSearch & "*"
.Offset(1,0).SpecialCells(xlCellTypeVisible).Cells.Copy
Sheets("Sheet5").activate
DestinationRange.PasteSpecial
End With
Posted
Updated 18-Mar-18 17:43pm

1 solution

Based on what you mentioned in the post, this is how the code should look like. the code need to
1. Find out how many rows need to compare
2. Loop each row, compare the Z column value in Sheet5 and Sheet1
3. If match, copied the cells A:Y from Sheet5 to Sheet1

Tested on Excel 2016.

VB
Sub CP_CopyPasteRow()
    Dim lastrow As Long

    'Disable Alert (Warning) Messages in Excel
    Application.DisplayAlerts = False
    
    With Sheets("Sheet5")
    'get the last row index with data
        lastrow = .Cells(.Rows.Count, "Z").End(xlUp).Row
        
        For r = 1 To lastrow
            'loop each to matzh Z column, if match copy A:Y to Sheet 1
            If Worksheets("Sheet1").Cells(r, Range("Z" & 1).Column).Value = .Cells(r, Range("Z" & 1).Column).Value Then
                .Rows(r).Copy
                Worksheets("Sheet1").Range("A" & r & ":Y" & r).PasteSpecial xlPasteValues
            End If
        Next r
        
    End With

'enable alert
   Application.DisplayAlerts = True
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900