Excel Formula to add lines dynamically based on the source data

Balajibenz

Board Regular
Joined
Nov 18, 2020
Messages
80
Office Version
  1. 2013
Platform
  1. Windows
Hi Team,

I have data in sheet1 as below.

KW2_Gagu Personal & Polen.xlsx
ABCDEFGHIJ
2FirmNumberName1/11/2021ZoneTotal
3Gagu Personal3439Adamcova, Milena14:1822:308.20.837.37OST1077.0
4Gagu Personal3441Balaz, Gabriel14:1922:187.980.837.15TRK571.0
5Gagu Personal3477Basno, Dorina14:0522:298.40.57.9TRK341.0
6Gagu Personal1938Belkania, Fridon14:2022:137.890.57.39#N/A#N/A
7Gagu Personal2435Berzan, Cristin3:4513:439.970.759.22TRK830.0
8Gagu Personal2482Bilici, Igor0:000:000.00.00.0MOP1453.0
9Gagu Personal1688Boji, Gheorghe3:5113:5910.130.839.3MOP1619.0
10Gagu Personal1681Boji, Romina3:4914:2310.570.759.82MOP2300.0
11Gagu Personal2436Boji, Ruslan3:5214:3010.630.759.88#N/A#N/A
12Gagu Personal3442Bozsuc, Vasile12:4721:599.20.838.37MOP1241.0
13Gagu Personal1849Brezovy, Adam5:1614:389.370.838.54TKK931.0
14Gagu Personal3475Burlacu, Andrian14:1722:178.00.57.5TRK410.0
Zeiterfassung


and data in sheet2 as below.

KW2_Gagu Personal & Polen.xlsx
ABCDE
1NameDateFirmZoneTotal
2Adamcova, Milena1/11/2021Gagu PersonalOST1077
3Adamcova, Milena1/11/2021Gagu PersonalTRK100
4Balaz, Gabriel1/11/2021Gagu PersonalTRK571
5Basno, Dorina1/11/2021Gagu PersonalTRK341
6Berzan, Cristin1/11/2021Gagu PersonalTRK830
7Bilici, Igor1/11/2021Gagu PersonalMOP1453
8Bilici, Igor1/11/2021Gagu PersonalTRK133
9Boji, Gheorghe1/11/2021Gagu PersonalMOP1619
10Boji, Romina1/11/2021Gagu PersonalMOP2300
11Bozsuc, Vasile1/11/2021Gagu PersonalMOP1241
12Brezovy, Adam1/11/2021Gagu PersonalTKK931
all picks


what i am looking for is to compare between two sheets and if there are mutliple lines for a person sheet2 then that line needs to added to sheet1.

for example take the first name from the list - Adamcova, Milena. we have one line for in sheet1 for the zone "OST" but in sheet2 we can two lines one for OST and other for "TRK". what I am looking for is to add this additional line(along with zone and total) from sheet2 to sheet1 right under the existing line (its not mandatory, we can add it in the last too so that i can sort it later).

there might more than 2 lines for person in few scenarios. thank you in advance.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try this:
VBA Code:
Sub ADDValuesAndRows()
Dim i As Long, j As Long, Lr1 As Long, Lr2 As Long, Sh1 As Worksheet, Sh2 As Worksheet
Dim K As Long, L1 As Long, L2 As Long
Set Sh1 = Sheets("Sheet1")
Set Sh2 = Sheets("Sheet2")
Lr1 = Sh1.Range("A" & Rows.Count).End(xlUp).Row
Lr2 = Sh2.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To Lr1 + Lr2
On Error Resume Next
K = Application.WorksheetFunction.Match(Sh1.Range("C" & i), Sh2.Range("A1:A" & Lr2), 0)
If K > 0 Then
Debug.Print K
L1 = Application.WorksheetFunction.CountIf(Sh2.Range("A2:A" & Lr1), Sh1.Range("C" & i).Value)
L2 = Application.WorksheetFunction.CountIf(Sh1.Range("C2:C" & Lr2), Sh1.Range("C" & i).Value)
Debug.Print L1
Debug.Print L2
End If
If L1 > L2 And L2 > 0 Then
Sh1.Rows(i + 1).Resize(L1 - L2).Insert
Sh1.Range("A" & i + 1 & ":A" & i + L1 - L2).Value = Sh2.Range("C" & K + 1 & ":C" & K + L1 - L2).Value
Sh1.Range("C" & i + 1 & ":C" & i + L1 - L2).Value = Sh2.Range("A" & K + 1 & ":A" & K + L1 - L2).Value
Sh1.Range("I" & i + 1 & ":I" & i + L1 - L2).Value = Sh2.Range("D" & K + 1 & ":D" & K + L1 - L2).Value
Sh1.Range("J" & i + 1 & ":J" & i + L1 - L2).Value = Sh2.Range("E" & K + 1 & ":E" & K + L1 - L2).Value
i = i + L1 - L2
End If
Next i
End Sub
 
Upvote 0
Solution
Try this:
VBA Code:
Sub ADDValuesAndRows()
Dim i As Long, j As Long, Lr1 As Long, Lr2 As Long, Sh1 As Worksheet, Sh2 As Worksheet
Dim K As Long, L1 As Long, L2 As Long
Set Sh1 = Sheets("Sheet1")
Set Sh2 = Sheets("Sheet2")
Lr1 = Sh1.Range("A" & Rows.Count).End(xlUp).Row
Lr2 = Sh2.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To Lr1 + Lr2
On Error Resume Next
K = Application.WorksheetFunction.Match(Sh1.Range("C" & i), Sh2.Range("A1:A" & Lr2), 0)
If K > 0 Then
Debug.Print K
L1 = Application.WorksheetFunction.CountIf(Sh2.Range("A2:A" & Lr1), Sh1.Range("C" & i).Value)
L2 = Application.WorksheetFunction.CountIf(Sh1.Range("C2:C" & Lr2), Sh1.Range("C" & i).Value)
Debug.Print L1
Debug.Print L2
End If
If L1 > L2 And L2 > 0 Then
Sh1.Rows(i + 1).Resize(L1 - L2).Insert
Sh1.Range("A" & i + 1 & ":A" & i + L1 - L2).Value = Sh2.Range("C" & K + 1 & ":C" & K + L1 - L2).Value
Sh1.Range("C" & i + 1 & ":C" & i + L1 - L2).Value = Sh2.Range("A" & K + 1 & ":A" & K + L1 - L2).Value
Sh1.Range("I" & i + 1 & ":I" & i + L1 - L2).Value = Sh2.Range("D" & K + 1 & ":D" & K + L1 - L2).Value
Sh1.Range("J" & i + 1 & ":J" & i + L1 - L2).Value = Sh2.Range("E" & K + 1 & ":E" & K + L1 - L2).Value
i = i + L1 - L2
End If
Next i
End Sub
thanks mate, that helped :-)
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,999
Members
452,373
Latest member
TimReeks

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