SOLVED

Copy Data once a cell changes and paste within a range on the next empty row

%3CLINGO-SUB%20id%3D%22lingo-sub-2739247%22%20slang%3D%22en-US%22%3ECopy%20Data%20once%20a%20cell%20changes%20and%20paste%20within%20a%20range%20on%20the%20next%20empty%20row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2739247%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20All%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20the%20following%20code%20to%20copy%20the%20range%20B%3AT%20once%20column%20P%20changes.%20(only%20the%20row%20the%20change%20was%20on.)%3C%2FP%3E%3CP%3EI%20would%20then%20like%20it%20to%20paste%20within%20range%20B5%3AB44%20on%20a%20different%20sheet%20on%20the%20next%20empty%20row%20but%20currently%20it%20only%20pastes%20B2%3AT5%20(Starting%20row%20is%20incorrect)%20and%20it%20will%20keep%20pasting%20over%20those%203%20rows%20only.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20anyone%20help%20me%20please%3F%20I%20have%20ten%20different%20ranges%20on%20the%20same%20sheet%20to%20paste%20in%20the%20next%20tab%20within%20it's%20own%20range%20but%20all%20of%20them%20are%20doing%20the%20same%20starting%20the%20paste%203%20rows%20higher%20and%20only%20pasting%20over%20the%20same%203%20rows.%20(Code%20is%20the%20same%20for%20each%20section%20so%20only%20pasted%20a%20snippet.)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20in%20advance%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EPrivate%20Sub%20Worksheet_Change(ByVal%20Target%20As%20Range)%0ADim%20r%20As%20Range%0ADim%20copysheet%20As%20Worksheet%0ADim%20Pastesheet%20As%20Worksheet%0A%0ASet%20copysheet%20%3D%20Worksheets(%22DH%20Write%20Offs%22)%0ASet%20Pastesheet%20%3D%20Worksheets(%22Written%20Off%22)%0A%0A%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%0A%20%20%20%20If%20Target.Column%20%3D%2016%20And%20Target.Row%20%26gt%3B%3D%205%20And%20Target.Row%20%26lt%3B%3D%2044%20Then%0A%20%20%20%20%20%20%20%20Set%20r%20%3D%20Target.Offset(%2C%20-14).Resize(%2C%2019)%0A%20%20%20%20%20%20%20%20copysheet.Range(r.Address).Copy%0A%20%20%20%20%20%20%20%20Pastesheet.Range(%22B5%3AB44%22).End(xlUp).Offset(1%2C%200).PasteSpecial%20xlPasteValues%0A%20%20%20%20%20%20%20%20Application.CutCopyMode%20%3D%20False%0A%20%20%20%20End%20If%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2739247%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2739524%22%20slang%3D%22en-US%22%3ERe%3A%20Copy%20Data%20once%20a%20cell%20changes%20and%20paste%20within%20a%20range%20on%20the%20next%20empty%20row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2739524%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3E%40Hans%20Vogelaar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20for%20the%20reply%20but%20unfortunately%20it%20doesn't%20work%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20attached%20an%20example%20spreadsheet%2C%20each%20section%20is%20it's%20own%20cost%20centre%20(Branch)%20and%20must%20only%20paste%20within%20the%20corresponding%20section%20on%20the%20Written%20Off%20tab.%20(All%20sections%20are%20the%20same%20size%20and%20format%20across%20both%20tabs.)%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2739559%22%20slang%3D%22en-US%22%3ERe%3A%20Copy%20Data%20once%20a%20cell%20changes%20and%20paste%20within%20a%20range%20on%20the%20next%20empty%20row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2739559%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1151959%22%20target%3D%22_blank%22%3E%40farley945%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThere%20was%20no%20way%20I%20could%20have%20guessed%20this%20from%20your%20first%20post...%3C%2FP%3E%0A%3CP%3EHere%20is%20a%20new%20version%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3EPrivate%20Sub%20Worksheet_Change(ByVal%20Target%20As%20Range)%0A%20%20%20%20Dim%20i%20As%20Long%0A%20%20%20%20Dim%20r%20As%20Range%0A%20%20%20%20Dim%20s%20As%20Long%0A%20%20%20%20Dim%20Pastesheet%20As%20Worksheet%0A%0A%20%20%20%20Set%20Pastesheet%20%3D%20Worksheets(%22Written%20Off%22)%0A%20%20%20%20For%20i%20%3D%205%20To%20392%20Step%2043%0A%20%20%20%20%20%20%20%20If%20Not%20Intersect(Range(%22P%22%20%26amp%3B%20i).Resize(40)%2C%20Target)%20Is%20Nothing%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20s%20%3D%20Pastesheet.Range(%22P%22%20%26amp%3B%20i%20%2B%2040).End(xlUp).Row%0A%20%20%20%20%20%20%20%20%20%20%20%20If%20s%20%26lt%3B%20i%20-%201%20Then%20s%20%3D%20i%20-%201%0A%20%20%20%20%20%20%20%20%20%20%20%20For%20Each%20r%20In%20Intersect(Range(%22P%22%20%26amp%3B%20i).Resize(40)%2C%20Target)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20s%20%3D%20s%20%2B%201%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Pastesheet.Range(%22B%22%20%26amp%3B%20s).Resize(1%2C%2019).Value%20%3D%20r.Offset(0%2C%20-14).Resize(1%2C%2019).Value%0A%20%20%20%20%20%20%20%20%20%20%20%20Next%20r%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20Next%20i%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
New Contributor

Hi All,

 

I have the following code to copy the range B:T once column P changes. (only the row the change was on.)

I would then like it to paste within range B5:B44 on a different sheet on the next empty row but currently it only pastes B2:T5 (Starting row is incorrect) and it will keep pasting over those 3 rows only.

 

Can anyone help me please? I have ten different ranges on the same sheet to paste in the next tab within it's own range but all of them are doing the same starting the paste 3 rows higher and only pasting over the same 3 rows. (Code is the same for each section so only pasted a snippet.)

 

Thank you in advance

 

 

Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range
Dim copysheet As Worksheet
Dim Pastesheet As Worksheet

Set copysheet = Worksheets("DH Write Offs")
Set Pastesheet = Worksheets("Written Off")

  Application.ScreenUpdating = False
  
    If Target.Column = 16 And Target.Row >= 5 And Target.Row <= 44 Then
        Set r = Target.Offset(, -14).Resize(, 19)
        copysheet.Range(r.Address).Copy
        Pastesheet.Range("B5:B44").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
        Application.CutCopyMode = False
    End If
End Sub

 

 

4 Replies

@farley945 

Try this:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim r As Range
    Dim s As Long
    Dim Pastesheet As Worksheet

    If Not Intersect(Range("P5:P44"), Target) Is Nothing Then
        Set Pastesheet = Worksheets("Written Off")
        s = Pastesheet.Range("B:T").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        If s < 4 Then s = 4
        For Each r In Intersect(Range("P5:P44"), Target)
            s = s + 1
            Pastesheet.Range("B" & s).Resize(1, 19).Value = r.Offset(0, -14).Resize(1, 19).Value
        Next r
    End If
End Sub

@Hans Vogelaar 

 

Thank you for the reply but unfortunately it doesn't work

 

I've attached an example spreadsheet, each section is it's own cost centre (Branch) and must only paste within the corresponding section on the Written Off tab. (All sections are the same size and format across both tabs.)

best response confirmed by farley945 (New Contributor)
Solution

@farley945 

There was no way I could have guessed this from your first post...

Here is a new version:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim i As Long
    Dim r As Range
    Dim s As Long
    Dim Pastesheet As Worksheet

    Set Pastesheet = Worksheets("Written Off")
    For i = 5 To 392 Step 43
        If Not Intersect(Range("P" & i).Resize(40), Target) Is Nothing Then
            s = Pastesheet.Range("P" & i + 40).End(xlUp).Row
            If s < i - 1 Then s = i - 1
            For Each r In Intersect(Range("P" & i).Resize(40), Target)
                s = s + 1
                Pastesheet.Range("B" & s).Resize(1, 19).Value = r.Offset(0, -14).Resize(1, 19).Value
            Next r
        End If
    Next i
End Sub
Apologies, I was going to repeat it for each section but yours works perfect thank you