Forum Discussion
Auto-populate sheet 2 when updates are made in sheet 1
You may place the following code on ThisWorkbook Module for the Workbook_SheetChange event.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
If Sh.Name <> "Apple" And Sh.Name <> "Orange" And Sh.Name <> "Cherry" Then Exit Sub
Dim Rng As Range
Dim dlr As Long
Dim r As Long
Dim n As Long
Dim dws As Worksheet
r = Target.Row
If r = 1 Then Exit Sub
Set dws = Worksheets("Fruits")
Set Rng = Union(Range("C" & r), Range("D" & r), Range("N" & r), Range("P" & r))
n = Application.CountA(Rng)
If n <> 4 Then Exit Sub
On Error GoTo Skip
Application.EnableEvents = False
dlr = dws.Cells(Rows.Count, "B").End(xlUp).Row + 1
dws.Range("B" & dlr).Value = Sh.Name
dws.Range("C" & dlr).Value = Range("C" & r).Value
dws.Range("D" & dlr).Value = Range("D" & r).Value
dws.Range("N" & dlr).Value = Range("N" & r).Value
dws.Range("P" & dlr).Value = Range("P" & r).Value
dws.Range("T" & dlr).Value = Range("T" & r).Value
Skip:
Application.EnableEvents = True
End Sub
As per the above code, once you fill all the four entries i.e. Count in column C, Date in column D, Name in column N and Price per count in column P on Apple or Orange or Cherry Tabs, the complete record will be automatically be copied to the Fruits Tab also.
Please find the attached with the code in place to test.
- rbbansalDec 05, 2021Copper Contributor
Thanks Subodh for your quick reply.
This is what I was looking for.
Sorry for confusion regarding the column headers. Name(N) was not column N but just to represent a variable for name .
There is some slight change in the file (please see attached) . I have sales and purchases in same workbook.
The tabs are S-Apples, S-Orange, S-Cherry and S-Fruits for sales
There are tabs P-Apples, P-Orange, P-Cherry and P-Fruits for purchases.
The requirement is that sales of individual fruits get consolidated in S-Fruits and similarly Purchases of Individual fruits gets consolidated in P-FruitsCan you help me with this
- Subodh_Tiwari_sktneerDec 06, 2021Silver Contributor
Make sure that all your S tabs and P tabs have a similar layout.
Please check the attached with the tweaked code in place.