插入复制的单元格值[vba]

时间:2018-07-04 14:44:45

标签: excel vba excel-vba

我有以下代码,它复制一定范围并粘贴将其插入下一个工作表,但它复制公式,我希望它插入复制的行数并仅粘贴值。

Sub create_payroll()
'copies values from 'Driver' Worksheet (till last row) and pastes values into Invoice Data A14
Dim LastRow As Long
Application.ScreenUpdating = False
LastRow = Worksheets("Driver").Cells(Rows.Count, "B").End(xlUp).Row
Sheets("Driver").Range("A3:H" & LastRow).Copy
Sheets("Invoice Data").Range("A14").Insert xlShiftDown

2 个答案:

答案 0 :(得分:3)

或完全绕过剪贴板:

Sheets("Invoice Data").Range("A14:H" & LastRow + 11).Value = Sheets("Driver").Range("A3:H" & LastRow).Value

要绕过剪贴板并插入行,请使用:

Sheets("Invoice Data").Rows("14:").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Sheets("Invoice Data").Range("A14:H" & LastRow + 11).Value = Sheets("Driver").Range("A3:H" & LastRow).Value

答案 1 :(得分:1)

尝试使用以下类似内容:

Sub create_payroll()
    'copies values from 'Driver' Worksheet (till last row) and pastes values into Invoice Data A14
    Dim LastRow As Long
    Dim srcRng As Range
    Application.ScreenUpdating = False

    With Sheets("Driver")
        LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
        Set srcRng = .Range("A3:H" & LastRow)
    End With


    With Sheets("Invoice Data")
        .Range("A14").Resize(srcRng.Rows.Count - 1, srcRng.Columns.Count).Insert shift:=xlDown
        .Range("A14").Resize(srcRng.Rows.Count, srcRng.Columns.Count).Value = srcRng.Value
    End With
End Sub