如何从特定列数的行中复制所有数据?

时间:2016-09-20 13:31:11

标签: excel vba

我将数据从一张表中的大表复制到一个较小的表中,以便仅显示请求供应商提供部件报价所需的必要数据行。我使用下面的代码:

Private Sub CommandButton1_Click()

Dim sh1 As Worksheet, sh2 As Worksheet, R As Integer
Set sh1 = Sheets("Breakdown & Analysis")
Set sh2 = Sheets("Mail Merge")
R = 2

For Each c1 In sh1.Range("O8", sh1.Range("O8").End(xlDown))
    If c1.Value = "Yes" Then     'If the part is specified as Yes to be quoted
        sh2.Rows(R) = c1.EntireRow.Value   'Copies entire row
        R = R + 1
    End If
Next

End Sub

我不想复制整行,而是留下评论说明这​​一点,我希望它只从该行传输1到20列。

如何编辑我的代码才能执行此操作?

1 个答案:

答案 0 :(得分:0)

Option Explicit

Sub main()
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim R As Long
    Dim c1 As Range

    Set sh1 = Worksheets("Breakdown & Analysis")
    Set sh2 = Worksheets("Mail merge")
    R = 2

    With sh1
        For Each c1 In .Range("O8", .Cells(.Rows.Count, "O").End(xlUp))
            If c1.Value = "Yes" Then
                sh2.Cells(R, 1).Resize(, 20).Value = .Cells(c1.Row, 1).Resize(, 20).Value
                R = R + 1
            End If
        Next c1
    End With
End Sub