Copying data from columns in 1 worksheet to another worksheet if a condition is met

Needinghlp

New Member
Joined
Feb 27, 2013
Messages
25
Hi VBA experts,

I have some code which is working, however I'm wondering if there is a more efficient code I could use. I have 2 worksheets (sheet 1 and sheet 2). Column J on sheet 2 has values of Yes or No. If column J has a yes I would like to copy the corresponding value in column A on sheet 2 to column b on sheet 1. I would also like to copy the corresponding values in columns C, D & E on sheet 1 to columns C, D & E on sheet 1. The code I'm using is below. Is there a way I can combine the columns I'm copying from in 1 line of code (and similarly combine the columns I'm pasting to in one row of code) i.e. Worksheets("Sheet2").Cells(i, 1, 3, 4, 5).Copy. This doesn't work - but hopefully it gives an idea of what I'm asking.

Private Sub CommandButton1_Click()
A = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row

For i = 3 To A

If Worksheets("Sheet2").Cells(i, 10).Value = "Yes" Then

Worksheets("Sheet2").Cells(i, 1).Copy
Worksheets("Sheet1").Cells(i, 2).Select
ActiveSheet.Paste
Worksheets("Sheet2").Cells(i, 3).Copy
Worksheets("Sheet1").Cells(i, 3).Select
ActiveSheet.Paste
Worksheets("Sheet2").Cells(i, 4).Copy
Worksheets("Sheet1").Cells(i, 4).Select
ActiveSheet.Paste

End If

Next

End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi
An option
VBA Code:
Private Sub CommandButton1_Click()
    A = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
    For i = 3 To a
        If Worksheets("Sheet2").Cells(i, 10).Value = "Yes" Then
            Worksheets("Sheet2").Cells(i, 1).Copy Worksheets("Sheet1").Cells(i, 2)
            Worksheets("Sheet2").Cells(i, 3).Copy Worksheets("Sheet1").Cells(i, 3)
            Worksheets("Sheet2").Cells(i, 4).Copy Worksheets("Sheet1").Cells(i, 4)
        End If
    Next
End Sub
 
Upvote 0
Another option
VBA Code:
Private Sub CommandButton1_Click()
   Dim UsdRws As Long, i As Long
   
   With Worksheets("Sheet2")
      UsdRws = .Cells(Rows.Count, 1).End(xlUp).Row
      For i = 3 To UsdRws
         If .Range("J" & i).Value = "Yes" Then
            .Range("A" & i).Copy Worksheets("Sheet1").Cells(i, 2)
            .Range("C" & i).Resize(, 3).Copy Worksheets("Sheet1").Cells(i, 3)
         End If
      Next i
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,865
Messages
6,121,988
Members
449,060
Latest member
mtsheetz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top