Worksheet Change Event to copy part of row to another sheet with values only

Jim885

Well-known Member
Joined
Jul 8, 2012
Messages
663
I have the below code that works. But as you can see, it uses two lines of code to copy the range of the row that is changed. I need help to make it copy the split range of the row using a single line of code ... and also copy the values only. Currently, this code copies the formula.

Could someone please tweak it?
Thanks!

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim dtmTime As Date
    Dim LR, LR2 As Long
    Dim myWorksheet As Worksheet
    Dim myWorksheetName As String
    myWorksheetName = Format(Now, "mmm yyyy")
    
    LR = Sheets(myWorksheetName).Range("A" & Rows.Count).End(xlUp).Row
    If Intersect(Target, Range("D2:E" & LR)) Is Nothing Then Exit Sub
    dtmTime = Now()
    
    LR2 = Sheets("Audit Sheet").Range("A" & Rows.Count).End(xlUp).Row + 1
    With Sheets("Audit Sheet")
        .Cells(LR2, 1) = dtmTime
        .Cells(LR2, 2) = Environ("USERNAME")
    End With
    
    'Need the below section to copy column A and columns C through F of the changed row onto the Audit sheet with a single line of code that also pastes values only
    Sheets(myWorksheetName).Range("A" & Target.Row).Copy Destination:=Sheets("Audit Sheet").Range("C" & LR2)
    Sheets(myWorksheetName).Range("C" & Target.Row & ":F" & Target.Row).Copy Destination:=Sheets("Audit Sheet").Range("D" & LR2)
    ThisWorkbook.Save
End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Since you are pasting to noncontiguous ranges, the copy and paste action cannot be made as one statement. The code below will paste values only.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim dtmTime As Date
    Dim LR, LR2 As Long
    Dim myWorksheet As Worksheet
    Dim myWorksheetName As String
    myWorksheetName = Format(Now, "mmm yyyy")    
    LR = Sheets(myWorksheetName).Range("A" & Rows.Count).End(xlUp).Row
    If Intersect(Target, Range("D2:E" & LR)) Is Nothing Then Exit Sub
    dtmTime = Now()    
    LR2 = Sheets("Audit Sheet").Range("A" & Rows.Count).End(xlUp).Row + 1
    With Sheets("Audit Sheet")
        .Cells(LR2, 1) = dtmTime
        .Cells(LR2, 2) = Environ("USERNAME")
    End With
    
    'Need the below section to copy column A and columns C through F of the changed row onto the Audit sheet with a single line of code that also pastes values only
    Sheets(myWorksheetName).Range("A" & Target.Row).Copy
    Sheets("Audit Sheet").Range("C" & LR2).PasteSpecial xlPasteValues
    Sheets(myWorksheetName).Range("C" & Target.Row & ":F" & Target.Row).Copy 
    Sheets("Audit Sheet").Range("D" & LR2).PasteSpecial xlPasteValues
    ThisWorkbook.Save
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,021
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top