从其他工作表复制VBA并粘贴为值

时间:2016-01-11 18:40:27

标签: excel-vba vba excel

我有一个宏,我通过一些例子,将值从所有其他工作表复制到工作表,称为"摘要"。值范围从A列到M,从第14行到结束。我是vba的新手,无法弄清楚如何修改此代码,因此它只会粘贴值。

  Sub copy_info()
    Dim i As Long, j As Long, lastRow As Long
    Dim sh As Worksheet
    With Sheets("Summary")
    End With
    j = 14
    For Each sh In ActiveWorkbook.Sheets
    If sh.Name <> "Summary" Then
        lastRow = sh.Cells(sh.Rows.Count, "B").End(xlUp).Row
        For i = 14 To lastRow
            If sh.Range("H" & i) > 0 Then
                sh.Range("A" & i & ":M" & i).Copy     Destination:=Worksheets("Summary").Range("A" & j)
                Sheets("Summary").Range("M" & j) = sh.Name
                j = j + 1
            End If
        Next i
    End If
    Next sh
    Sheets("Summary").Columns("A:M").AutoFit
    End Sub

1 个答案:

答案 0 :(得分:3)

更改此行:

sh.Range("A" & i & ":M" & i).Copy     Destination:=Worksheets("Summary").Range("A" & j)

至2行:

sh.Range("A" & i & ":M" & i).Copy     
Worksheets("Summary").Range("A" & j).PasteSpecial xlPasteValues

或者,你可以写

Worksheets("Summary").Range("A" & j ":M" & j).Value = sh.Range("A" & i & ":M" & i).Value