yoyo
yoyo

Reputation: 59

Paste row based on criteria

I found posts based on copying based on criteria. In my case the pasting must be done based on a criteria.

I have a workbook which has two sheets- Data and Agenda.

Within the Agenda sheet, I have a row A3 : Y3 that has values that value is specific for a certain key value.
Agenda worksheet Screenshot

When I press the update button, I want the data from Agenda Sheet (A3 : Y3) to be copied to the Data sheet that has the key value in column C.
Data worksheet Screenshot

What I want to achieve is, as in database for a table 'A' we have primary key as certain unique column and based on that we update/insert data in table 'B' where that unique column is foreign key.

I have been able to copy/paste in the desired columns, but unable to perform pasting per criteria.

Private Sub CommandButton1_Click()
    
    a = Worksheets("Data").Cells(Rows.Count, 3).End(xlUp).Row
    
    For i = 4 To a
        If Worksheets("Sheet4").Cells(i, 3).Value = Worksheets("Agenda").Cells(4, 4).Value Then    
            Sheets("Agenda").Range("A3:Y3").Copy
            Sheets("Data").Range("G4").PasteSpecial Paste:=xlPasteValues
            MsgBox ("Done")
        End If
    Next
    
End Sub

Upvotes: 0

Views: 112

Answers (1)

yoyo
yoyo

Reputation: 59

Got it to work.

In code line 6 I replaced ("G4") with ("G" & i). It produced the desired result

Upvotes: 1

Related Questions