VBA:根据单元格的值将工作表1中的单元格复制到工作表2

时间:2017-01-27 20:12:44

标签: vba excel-vba excel

使用VBA,我该如何:

仅当单元格A1(在Worksheet1中)值=“YES”时,才将Worksheet1中的单元格B1复制到新的Worksheet2。

然后对范围A1中的每一行重复:A1000(即基于A2值的B2副本=“是”等等) - 如果单元格为空,请检查下一行。

Sub Output()
Dim c As Range
    Dim j As Integer
    Dim Source As Worksheet
    Dim Target As Worksheet

    Set Source = ActiveWorkbook.Worksheets("worksheet1")
    Set Target = ActiveWorkbook.Worksheets("worksheet2")

    j = 1     ' Start copying to row 1 in target sheet
    For Each c In Source.Range("R2:R1000")   ' Do 1000 rows
        If c = "YES" Then
           Source.Rows(c.Row).Copy Target.Rows(j) /*getting stuck here on trying to copy a cell from column S in worksheet1 to worksheet2
           j = j + 1
        End If
    Next c
End Sub

1 个答案:

答案 0 :(得分:1)

您只需要快速修改即可将行复制到单个单元格和位置。我用过你从c行的AG栏复制。

Sub Output()
Dim c As Range
Dim j As Integer
Dim Source As Worksheet
Dim Target As Worksheet

Set Source = ActiveWorkbook.Worksheets("workshee1")
Set Target = ActiveWorkbook.Worksheets("workshee2")

j = 1     ' Start copying to row 1 in target sheet
    For Each c In Source.Range("R2:R1000")   ' Do 1000 rows
        If c = "YES" Then
            Target.Cells(j, "A").Value = Source.Cells(c.Row, "AG").Value
            j = j + 1
        End If
    Next c
End Sub