How can I automate copying rows to another tab BUT always add the newest additions to the end?

steve80s

New Member
Joined
Aug 18, 2022
Messages
11
Office Version
  1. 2016
Platform
  1. Windows
Hi All,
Sorry if that isn't very clear. What I have is a spreadsheet where I have a MAIN LOG, which I use to keep track of client appointment and report timescales. That all works fine, so don't worry too much about that. What I need help with is this. When 'Y' is entered in Column R (indicating a blood test has been requested), I need a way for Excel to automatically copy information from that row to my BLOODS tab. For example, if I enter Y in R7 of the MAIN LOG, I need parts of Row 7 to copy over parts of this row to Row 4 Columns B-H on the BLOODS tab. After that, additional info on the bloods progress can be manually entered in columns I onwards.

I have found an INDEX formula which populates this. However, let's say I've already had Rod Flanders (Row 7) copied over to Row 4 of BLOODS, but then someone adds a Y to MAIN LOG Row 5 (Lisa Simpson's row). What happens is the BLOODS tab adds Lisa's data to Row 4, and moves Rod Flanders down to Row 5. This is a problem, because if I have added manual data to Rod's row already in Columns I, J, K etc, this data will stay where it is, even though Rod's other details will be on a different row now, and it looks like that data related to Lisa Simpson.

I suppose what I need is for each time MAIN LOG Column R has a Y put in it, it sends a permanent copy to the next free row of the BLOODS tab, which doesn't get re-ordered when another MAIN LOG client has a test requested. ie. if Rod Flanders gets copied to BLOODS Row 4, it stays there, even if Bart Simpson and Lisa Simpson subsequently have bloods requested. They should be copied to Rows 5 and 6 as they had the Column R 'Y' added after Rod Flanders.

Hope that makes sense? Please see screenshots below of my 2 different tabs

Main Log.JPG


Bloods.JPG
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Book5
ABCDEFGHIJKLMNOPQR
3Current Days WaitingTarget DateCurrent StatusFinal Compclinic typeclinic dateAttended or DNAclinicianClient no.Client NameDobdate ddate rdate fFormatted admin namesign off datedate sentbloods requested
4576/2/2023Complete3Neuro6/2/2023AttendedHomer101Robert1/1/20101/1/20101/1/20101/1/2010abcY
5586/3/2023Complete2Dcd6/3/2023AttendedR102Nobert2/1/20102/1/20102/1/20102/1/2010dfe
6596/4/2023Complete1jac6/4/2023Attendedweer103Sobert3/1/20103/1/20103/1/20103/1/2010abc
7606/5/2023Send2vd6/5/2023AttendedTeres104Labert4/1/20104/1/20104/1/20104/1/2010dfeY
8606/6/2023Pend3w6/6/2023AttendedFellex107Pabert5/1/20105/1/20105/1/20105/1/2010dfe
916/7/2023Complete3we6/7/2023AttendedSalles105Wabert6/1/20106/1/20106/1/20106/1/2010abc
1026/8/2023Pend4r6/8/2023AttendedMales106Sur7/1/20107/1/20107/1/20107/1/2010dfeY
1126/8/2023Pend4r6/8/2023AttendedMales106Sur7/1/20107/1/20107/1/20107/1/2010dfeY
MainLog
Cells with Conditional Formatting
CellConditionCell FormatStop If True
F3:R3,E1:E11Cell ValueduplicatestextNO


Book5
BCDEFGHIJ
2C Details
3Clinic DateClinic typeClinicianNHS NoClient NameDate of BirthadminBloods donedate of test
46/2/2023AttendedHomer101Robert1/1/2010
56/5/2023AttendedTeres104Labert4/1/2010
66/8/2023AttendedMales106Sur7/1/2010
76/8/2023AttendedMales106Sur7/1/2010
Bloods


If using VBA, Right Click MainLog -> View code -> paste below code and run the code (f5)

VBA Code:
Sub test()

With Sheets("MainLog")
a = .Range("f3:r" & .Cells(Rows.Count, "A").End(xlUp).Row).Value
End With

ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))

For I = 1 To UBound(a, 1)
    If UCase(a(I, 13)) = "Y" Then
        k = k + 1
        For j = 1 To 6
            b(k, j) = a(I, j)
        Next j
    End If

Next I

With Sheets("Bloods")
.Range("b4").Resize(UBound(b, 1), UBound(b, 2) - 4).Value = b
End With

End Sub
 
Upvote 0
Hi RudRud,

Thank you for responding. However, there are 3 problems with this...

1 - When I run the VBA, it deletes and repopulates the list in BLOODS. What I need is it to leave clients already on the BLOODS, and just search for additional ones to add to the end
2 - While deleting and repopulating BLOODS, it also deletes the manual data I'm adding in Columns I onwards.
3 - The BLOODS Clinic Type column is being populated with the MAIN LOG 'Attended or DNA' column data rather than the Clinic Type data.

Is it possible to fix this please?
 
Upvote 0
Kindly give a try

Book5
ABCDEFGHIJKLMNOPQR
3Current Days WaitingTarget DateCurrent StatusFinal Completion Timeclinic typeclinic dateAttended or DNAclinicianClient no.Client NameD.o.b.Date DictatedDate RosettedDate FormattedFormatted by(Admin Name)Sign-Off DateDate SentBloods Requested
4576/2/2023Complete3Neuro6/2/2023AttendedHomer101Robert1/1/20101/1/20101/1/20101/1/2010abcY
5586/3/2023Complete2Dcd6/3/2023AttendedR102Nobert2/1/20102/1/20102/1/20102/1/2010dfe
6596/4/2023Complete1jac6/4/2023Attendedweer103Sobert3/1/20103/1/20103/1/20103/1/2010abc
7606/5/2023Send2vd6/5/2023AttendedTeres104Labert4/1/20104/1/20104/1/20104/1/2010dfeY
8606/6/2023Pend3w6/6/2023AttendedFellex107Pabert5/1/20105/1/20105/1/20105/1/2010dfe
916/7/2023Complete3we6/7/2023AttendedSalles105Wabert6/1/20106/1/20106/1/20106/1/2010abc
1026/8/2023Pend4r6/8/2023AttendedMales106Sur7/1/20107/1/20107/1/20107/1/2010dfeY
1126/8/2023Pend4r6/8/2023AttendedMales106Sur7/1/20107/1/20107/1/20107/1/2010dfeY
MainLog
Cells with Conditional Formatting
CellConditionCell FormatStop If True
E3:R3Cell ValueduplicatestextNO


Book5
BCDEFGHIJ
2C Details
3Clinic DateClinic typeClinicianNHS NoClient NameDate of BirthadminBloods donedate of test
46/2/2023NeuroHomer101Robert1/1/2010
56/5/2023vdTeres104Labert4/1/2010
66/8/2023rMales106Sur7/1/2010
76/8/2023rMales106Sur7/1/2010
Bloods


VBA Code:
Sub test()
With Sheets("MainLog")
a = .Range("e3:r" & .Cells(Rows.Count, "A").End(xlUp).Row).Value
End With

ReDim b(1 To UBound(a, 1), 1 To 6)

For i = 1 To UBound(a, 1)
    If UCase(a(i, 14)) = "Y" Then
        k = k + 1
            b(k, 1) = a(i, 2)
            b(k, 2) = a(i, 1)
            b(k, 3) = a(i, 4)
            b(k, 4) = a(i, 5)
            b(k, 5) = a(i, 6)
            b(k, 6) = a(i, 7)
    End If
Next i



Sheets("bloods").Range("b4").Resize(UBound(a, 1), 6).Value = b


End Sub
 
Upvote 0
Kindly give a try

Book5
ABCDEFGHIJKLMNOPQR
3Current Days WaitingTarget DateCurrent StatusFinal Completion Timeclinic typeclinic dateAttended or DNAclinicianClient no.Client NameD.o.b.Date DictatedDate RosettedDate FormattedFormatted by(Admin Name)Sign-Off DateDate SentBloods Requested
4576/2/2023Complete3Neuro6/2/2023AttendedHomer101Robert1/1/20101/1/20101/1/20101/1/2010abcY
5586/3/2023Complete2Dcd6/3/2023AttendedR102Nobert2/1/20102/1/20102/1/20102/1/2010dfe
6596/4/2023Complete1jac6/4/2023Attendedweer103Sobert3/1/20103/1/20103/1/20103/1/2010abc
7606/5/2023Send2vd6/5/2023AttendedTeres104Labert4/1/20104/1/20104/1/20104/1/2010dfeY
8606/6/2023Pend3w6/6/2023AttendedFellex107Pabert5/1/20105/1/20105/1/20105/1/2010dfe
916/7/2023Complete3we6/7/2023AttendedSalles105Wabert6/1/20106/1/20106/1/20106/1/2010abc
1026/8/2023Pend4r6/8/2023AttendedMales106Sur7/1/20107/1/20107/1/20107/1/2010dfeY
1126/8/2023Pend4r6/8/2023AttendedMales106Sur7/1/20107/1/20107/1/20107/1/2010dfeY
MainLog
Cells with Conditional Formatting
CellConditionCell FormatStop If True
E3:R3Cell ValueduplicatestextNO


Book5
BCDEFGHIJ
2C Details
3Clinic DateClinic typeClinicianNHS NoClient NameDate of BirthadminBloods donedate of test
46/2/2023NeuroHomer101Robert1/1/2010
56/5/2023vdTeres104Labert4/1/2010
66/8/2023rMales106Sur7/1/2010
76/8/2023rMales106Sur7/1/2010
Bloods


VBA Code:
Sub test()
With Sheets("MainLog")
a = .Range("e3:r" & .Cells(Rows.Count, "A").End(xlUp).Row).Value
End With

ReDim b(1 To UBound(a, 1), 1 To 6)

For i = 1 To UBound(a, 1)
    If UCase(a(i, 14)) = "Y" Then
        k = k + 1
            b(k, 1) = a(i, 2)
            b(k, 2) = a(i, 1)
            b(k, 3) = a(i, 4)
            b(k, 4) = a(i, 5)
            b(k, 5) = a(i, 6)
            b(k, 6) = a(i, 7)
    End If
Next i



Sheets("bloods").Range("b4").Resize(UBound(a, 1), 6).Value = b


End Sub
Hi,

I tried the new code. Unfortunately it still keeps pasting the rows into the BLOODS sheet in the original order they were in on the MAIN LOG. I really need it to look for new additions (which may be near the top or bottom of the MAIN LOG and copy the new rows to the end of the BLOODS sheet, otherwise the manual notes in BLOODS Columns I to N just stay where they were and end up next to the wrong client.

Also, it's no longer populating Column H (Admin).

Any more suggestions please?
 
Upvote 0
How about this?

Book2
ABCDEFGHIJKLMNOPQR
1Current Days WaitingTarget DateCurrent StatusFinal Completion Timeclinic typeclinic dateAttended or DNAclinicianClient no.Client NameD.o.b.Date DictatedDate RosettedDate FormattedFormatted by(Admin Name)Sign-Off DateDate SentBloods Requested
2576/2/23Complete3Neuro6/2/23AttendedHomer101Robert1/1/101/1/101/1/101/1/10abcY
3586/3/23Complete2Dcd6/3/23AttendedR102Nobert2/1/102/1/102/1/102/1/10dfe
4596/4/23Complete1jac6/4/23Attendedweer103Sobert3/1/103/1/103/1/103/1/10abc
5606/5/23Send2vd6/5/23AttendedTeres104Labert4/1/104/1/104/1/104/1/10dfeY
6606/6/23Pend3w6/6/23AttendedFellex107Pabert5/1/105/1/105/1/105/1/10dfe
716/7/23Complete3we6/7/23AttendedSalles105Wabert6/1/106/1/106/1/106/1/10abc
826/8/23Pend4r6/8/23AttendedMales106Sur7/1/107/1/107/1/107/1/10dfeY
926/8/23Pend4r6/8/23AttendedMales106TES7/1/107/1/107/1/107/1/10dfeY
1026/8/23Pend4r6/8/23AttendedMales106Mrexcel7/1/107/1/107/1/107/1/10dfeY
MainLog


Book2
ABCDEFGHI
1C Details
2Clinic DateClinic typeClinicianNHS NoClient NameDate of BirthBloods donedate of test
36/2/23NeuroHomer101Robert1/1/10
46/5/23vdTeres104Labert4/1/10
56/8/23rMales106Sur7/1/10
66/8/23rMales106TES7/1/10
76/8/23rMales106Mrexcel7/1/10
Bloods


VBA Code:
Sub test2()
Dim dict As New Dictionary
dict.CompareMode = TextCompare

c = Sheets("Bloods").Range("f3:f" & Cells(Rows.Count, "E").End(xlUp).Row).Value
For i = 1 To UBound(c, 1)
If Not dict.Exists(c(i, 1)) Then
    dict.Add c(i, 1), i
End If
Next i

With Sheets("MainLog")
a = .Range("e2:r" & .Cells(Rows.Count, "j").End(xlUp).Row).Value
End With

For i = 1 To UBound(a, 1)
    If Not dict.Exists(a(i, 6)) And UCase(a(i, 14)) = "Y" Then
        With Sheets("bloods")
           lrow = .Cells(Rows.Count, "f").End(xlUp).Row + 1
            .Cells(lrow, "b") = a(i, 2)
           .Cells(lrow, "c") = a(i, 1)
            .Cells(lrow, "d") = a(i, 4)
            .Cells(lrow, "e") = a(i, 5)
            .Cells(lrow, "f") = a(i, 6)
            .Cells(lrow, "g") = a(i, 7)
        End With
    End If
Next i
End Sub
 
Upvote 0
Ok, that's working perfectly. Thank you. Last thing to do, can we automate the script to run any time something is entered into ROW R of the MAIN LOG please? As the other people using this form will not know how to run it.
 
Upvote 0
Hi @steve80s, Right Click MainLog -> View Code -> Paste the code below

The code will run automatically when user input "Y" in Mainlog sheets column R

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
    If Target.Column = 18 And UCase(Target.Value) = "Y" Then
    Call test2
    End If
End Sub

Sub test2()
Dim dict As New Dictionary
dict.CompareMode = TextCompare

c = Sheets("Bloods").Range("f3:f" & Cells(Rows.Count, "E").End(xlUp).Row).Value
For i = 1 To UBound(c, 1)
If Not dict.Exists(c(i, 1)) Then
    dict.Add c(i, 1), i
End If
Next i

With Sheets("MainLog")
a = .Range("e2:r" & .Cells(Rows.Count, "j").End(xlUp).Row).Value
End With

For i = 1 To UBound(a, 1)
    If Not dict.Exists(a(i, 6)) And UCase(a(i, 14)) = "Y" Then
        With Sheets("bloods")
           lrow = .Cells(Rows.Count, "f").End(xlUp).Row + 1
            .Cells(lrow, "b") = a(i, 2)
           .Cells(lrow, "c") = a(i, 1)
            .Cells(lrow, "d") = a(i, 4)
            .Cells(lrow, "e") = a(i, 5)
            .Cells(lrow, "f") = a(i, 6)
            .Cells(lrow, "g") = a(i, 7)
        End With
    End If
Next i

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

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