简体   繁体   中英

Copy and paste based on criteria

I'm trying to write a script in a workbook that will check a separate worksheet ("Worksheet 2") in row 10 for a value ("Yes") - See Worksheet 2 Column B

If it returns with a yes I want to copy the cell in Row 4 in the same column (in this case B4) and paste it to "Worksheet 1" in column B starting on Row 2 - See Worksheet 1 Column B

Then I want it to check the next 100 rows for the same info and paste all of the Row 4 Cells that have yes from Row 10 into "Worksheet 1 - Row B". Here is what I currently have:

Sub CommandButton1_Click()

Dim lr As Long
lr = Cells(Rows.Count, "B").End(xlUp).Row
For Each Sheets("Option 2").Cell In Range("B10:CZ10")
        If Cell.Value = Range("Yes").Value Then
       Worksheets("Option 2").Cell.Range(B4).Copy Range("B")
       Exit Sub
    End If
Next Cell
    Worksheets("Option 2").Range("B5:CW10").Copy Range("B")
    Range("B" & lr + 1).PasteSpecial Paste:=xlPasteValues
End Sub

Sorry if this is written horribly. I'm new to VBA and don't have a lot of guidance. Any help will be greatly appreciated!

Perhaps this is what you are attempting.

Sub CommandButton1_Click()
    Dim lr As Long, rng as range

    lr = Cells(Rows.Count, "B").End(xlUp).Row

    For Each rng In Sheets("Option 2").Range("B10:CZ10")
        If lcase(rng.Value) = "yes" Then
            lr = lr+1
            cells(lr, "B") = rng.offset(-6, 0).value
        End If
    Next rng

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM