根据A列中的值将指定的列复制到工作表

时间:2016-10-31 12:11:24

标签: excel-vba vba excel

我有以下工作正常但不是将整个行从“合并”工作表复制到“摘要”工作表我只想将列A复制到T.这是第一次尝试所以任何帮助都会感激不尽接收!

`Private Sub CommandButton1_Click()

'Define Variables
Dim DestSh As Worksheet
Dim s As Worksheet
Dim c As Integer
Dim i
Dim LastRow

With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With

'Delete the Combined sheet if it exists
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("Combined").Delete
On Error GoTo 0
Application.DisplayAlerts = True

'Add a new Combined worksheet
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = "Combined"

'Select Summary worksheet and copy headings and column widths to Combined worksheet
Sheets("Summary").Activate
Range("A24").EntireRow.Select
Selection.Copy Destination:=Sheets("Combined").Range("A1")
For c = 1 To Sheets("Summary").Columns.Count
    Sheets("Combined").Columns(c).ColumnWidth = Sheets("Summary").Columns(c).ColumnWidth
Next

'Loop through all worksheets sheets that begin with ra
'and copy to the combined worksheet
For Each s In ActiveWorkbook.Sheets
    If LCase(Left(s.Name, 2)) = "ra" Then
    Application.Goto Sheets(s.Name).[A1]
    Selection.Range("A23:Q50").Select
    Selection.Copy Destination:=Sheets("Combined"). _
    Cells(Rows.Count, 1).End(xlUp)(2)

End If

Next

        'Copy all rows that contain Yes in column A to Summary worksheet
        LastRow = Sheets("Combined").Range("A" & Rows.Count).End(xlUp).Row
        Sheets("Summary").Range("A25:V500").ClearContents
        For i = 1 To LastRow
            If Sheets("Combined").Cells(i, "A").Value = "Yes" Then
            Sheets("Combined").Cells(i, "A").EntireRow.Copy Destination:=Sheets("Summary").Range("A" & Rows.Count).End(xlUp).Offset(1)
            End If
            Next i

'Force return to Summary worksheet
Worksheets("Summary").Activate
End Sub

1 个答案:

答案 0 :(得分:0)

您可以使用.Resize()方法更改复制的范围。将您复制的行替换为使用此行并将其粘贴到新目标位置,它应该有效:

Sheets("Combined").Cells(i, "A").Resize(1, 20).Copy Destination:=Sheets("Summary").Range("A" & Rows.Count).End(xlUp).Offset(1)