简体   繁体   中英

insert copied cell values [vba]

I have the following code it copies a certain range and paste inserts it in the next worksheet, but it copies formulas, I want it to insert the amount of rows copied and paste values only.

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

Or bypass the clipboard entirely:

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

To bypass the clipboard and insert rows use:

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

Try using something like the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM