subtraction & summing and calculation for each two sheets together

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
399
Office Version
  1. 2016
Platform
  1. Windows
hi
I want subtracting specific two sheets together and give me the filnal result for all of the sheets by calculation
should subtract sheet STA from sheet RPA and show the result in sheet NET PUR (the result depends on column B when subtract the items for columns QTY & TOTAL)
Result.xlsx
ABCDEFG
1ITEMCO-ITFOODTT-MMNORT-WWQTYTOTAL
21COR-FF1FRBANANATT400.00TRY 8,200.00
32COR-FF2FRAPPLELL100.00TRY 10,000.00
43COR-FF3FRPEARNN10.00TRY 100.00
54COR-FF4FRBANANAQQ20.00TRY 400.00
65COR-FF5VEGTOMATOSS12.00TRY 144.00
76COR-FF6VEGTOMATOAA12.00TRY 144.00
STA


Result.xlsx
ABCDEFG
1ITEMCO-ITFOODTT-MMNORT-WWQTYTOTAL
21COR-FF11FRPEACHTT320.00TRY 4,840.00
32COR-FF12FRAPPLELL130.00TRY 2,990.00
43COR-FF1FRBANANATT300.00TRY 8,000.00
54COR-FF14FRBANANAQQ300.00TRY 6,600.00
65COR-FF4FRBANANAQQ30.00TRY 600.00
76COR-FF16VEGTOMATOAA24.00TRY 264.00
RPA


RESULT
Result.xlsx
ABCDEFG
1ITEMCO-ITFOODTT-MMNORT-WWQTYTOTAL
21COR-FF1FRBANANATT100.00TRY 200.00
32COR-FF2FRAPPLELL100.00TRY 10,000.00
43COR-FF3FRPEARNN10.00TRY 100.00
54COR-FF4FRBANANAQQ10.00-TRY 200.00
65COR-FF5VEGTOMATOSS12.00TRY 144.00
76COR-FF6VEGTOMATOAA12.00TRY 144.00
87COR-FF11FRPEACHTT320.00TRY 4,840.00
98COR-FF12FRAPPLELL130.00TRY 2,990.00
109COR-FF14FRBANANAQQ300.00TRY 6,600.00
1110COR-FF16VEGTOMATOAA24.00TRY 264.00
NET PUR



should subtract sheet SR from sheet SS and show the result in sheet NET SUR (the result depends on column B when subtract the items for columns QTY & TOTAL)



Result.xlsx
ABCDEFG
1ITEMCO-ITFOODTT-MMNORT-WWQTYTOTAL
21COR-FF12FRAPPLELL5.00TRY 115.00
32COR-FF13FRPEARNN4.00TRY 48.00
43COR-FF11FRPEACHTT11.00TRY 242.00
SR



Result.xlsx
ABCDEFG
1ITEMCO-ITFOODTT-MMNORT-WWQTYTOTAL
21COR-FF4FRBANANAQQ2.00TRY 40.00
32COR-FF13FRPEARNN8.00TRY 96.00
43COR-FF6VEGTOMATOAA4.00TRY 48.00
SS



RESULT


Result.xlsx
ABCDEFG
1ITEMCO-ITFOODTT-MMNORT-WWQTYTOTAL
21COR-FF4FRBANANAQQ2.00TRY 40.00
32COR-FF6VEGTOMATOAA4.00TRY 48.00
43COR-FF12FRAPPLELL5.00TRY 115.00
54COR-FF13FRPEARNN-4.00-TRY 48.00
65COR-FF11FRPEACHTT11.00TRY 242.00
NET SUR


should sum sheet FRS with sheet NET PUR and subtract from sheet NET SUR (the result depends on column B when sum & subtract the items for columns QTY & TOTAL)

Result.xlsx
ABCDEFG
1ITEMCO-ITFOODTT-MMNORT-WWQTYTOTAL
21COR-FF1FRBANANATT400.00TRY 8,200.00
32COR-FF2FRAPPLELL100.00TRY 10,000.00
43COR-FF3FRPEARNN10.00TRY 100.00
54COR-FF4FRBANANAQQ20.00TRY 400.00
65COR-FF5VEGTOMATOSS12.00TRY 144.00
76COR-FF6VEGTOMATOAA12.00TRY 144.00
87COR-FF11FRPEACHTT320.00TRY 4,840.00
98COR-FF12FRAPPLELL130.00TRY 2,990.00
109COR-FF13FRPEARNN4.00TRY 48.00
1110COR-FF14FRBANANAQQ300.00TRY 6,600.00
1211COR-FF16VEGTOMATOAA24.00TRY 264.00
1312COR-FF17VEGTOMATOAA125.00TRY 265.00
1413COR-FF18VEGTOMATOAA226.00TRY 266.00
FRS



RESULT
Result.xlsx
ABCDEFG
1ITEMCO-ITFOODTT-MMNORT-WWQTYTOTAL
21COR-FF1FRBANANATT500.00TRY 8,400.00
32COR-FF2FRAPPLELL200.00TRY 20,000.00
43COR-FF3FRPEARNN20.00TRY 200.00
54COR-FF4FRBANANAQQ28.00TRY 160.00
65COR-FF5VEGTOMATOSS24.00TRY 288.00
76COR-FF6VEGTOMATOAA20.00TRY 240.00
87COR-FF11FRPEACHTT629.00TRY 9,398.00
98COR-FF12FRAPPLELL255.00TRY 5,865.00
109COR-FF13FRPEARNN8.00TRY 96.00
1110COR-FF14FRBANANAQQ600.00TRY 13,200.00
1211COR-FF16VEGTOMATOAA48.00TRY 264.00
1312COR-FF17VEGTOMATOAA125.00TRY 265.00
1413COR-FF18VEGTOMATOAA226.00TRY 266.00
FINAL

should show all of items (some items are existe in sheet but not existed in another.
 
Code modified.
VBA Code:
Sub GetDifference()
Dim S As String, S1 As String
Dim Sh1 As Worksheet, Sh2 As Worksheet, Sh3 As Worksheet
Dim T1 As Long, T2 As Long, Cnt As Long
Dim A, B, N, P1, P2
Dim Dic As Object
' Sheet names are: NET PUR, NET SUR, FINAL
Set Sh1 = Sheets("NET PUR"): Set Sh2 = Sheets("NET SUR"): Set Sh3 = Sheets("FINAL")
A = Sh1.Range("A1").CurrentRegion
B = Sh2.Range("A1").CurrentRegion

With CreateObject("Scripting.dictionary")

For T1 = 2 To UBound(A, 1)
.Add A(T1, 2) & "_" & A(T1, 3) & "_" & A(T1, 4) & "_" & A(T1, 5), A(T1, 6) & "_" & Replace(Replace(A(T1, 7), "TRY ", ""), ",", "")
Next T1
Cnt = UBound(A, 1) - 1

For T2 = 2 To UBound(B, 1)
S = B(T2, 2) & "_" & B(T2, 3) & "_" & B(T2, 4) & "_" & B(T2, 5)
If .exists(S) Then
N = Split(.Item(S), "_")
S1 = Val(N(0)) - B(T2, 6) & "_" & Val(N(1)) - Val(Replace(Replace(B(T2, 7), "TRY ", ""), ",", ""))
.Item(S) = S1
Else
.Add B(T2, 2) & "_" & B(T2, 3) & "_" & B(T2, 4) & "_" & B(T2, 5), B(T2, 6) & "_" & Replace(Replace(B(T2, 7), "TRY ", ""), ",", "")
Cnt = Cnt + 1
End If
Next T2

P1 = .keys
P2 = .items
End With
If Cnt > 0 Then
With Sh3
.Range("A1").CurrentRegion.Clear
    With .Range("B2").Resize(Cnt, 1)
    .Value = WorksheetFunction.Transpose(P1)
    .TextToColumns Destination:=Range("B2"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
        :="_", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1)), _
        TrailingMinusNumbers:=True
    End With
    With .Range("F2").Resize(Cnt, 1)
    .Value = WorksheetFunction.Transpose(P2)
    .TextToColumns Destination:=Range("F2"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
        :="_", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1)), _
        TrailingMinusNumbers:=True
    End With
    With .Range("A2").Resize(Cnt, 1)
    .Formula = "=row(A1)"
    .Value = .Value
    End With
    With .Range("H2").Resize(Cnt, 1)
    .Formula = "=IF(SIGN(G2)=-1,""-"","""")&""TRY ""&ABS(G2)&"".00"""
    .Offset(0, -1).Value = .Value
    .Clear
    .Offset(0, -2).NumberFormat = "0.00"
    End With
.Range("A1:G1") = Array("ITEM", "CO-IT", "FOOD", "TT-MMN", "ORT-WW", "QTY", "TOTAL")
    With .Range("A1").CurrentRegion
    .Borders.LineStyle = xlContinuous
    .EntireColumn.AutoFit
    .HorizontalAlignment = xlCenter
    End With
End With
End If
End Sub
 
Upvote 0

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
ok this is the file
in sheets NET PUR , NET SUR, FINAL contain the headers supposes calculation based on your code and what I posted above
data.xlsm
 
Upvote 0
What about the code I have given in post 11.
sorry I don't note it . I tested and unfortunately doesn't show anything and there is no error at all:unsure:
 
Upvote 0
Rewritten the code;
VBA Code:
Sub GetDifference()
Dim Sh1 As Worksheet, Sh2 As Worksheet, Sh3 As Worksheet
Dim Rnds As Long, T1 As Long, T2 As Long, T3 As Long, Cnt As Long, k As Long
Dim temp As String, S As String
Dim A, B, C
Dim Dic(1 To 4) As Object
Dim shary

shary = Array("SR", "SS", "NET SUR", "STA", "RPA", "NET PUR", "NET SUR", "NET PUR", "FINAL")
For k = 1 To 4
Set Dic(k) = CreateObject("Scripting.dictionary")
Next k
For Rnds = 1 To 3
If Rnds = 1 Then k = 1 Else k = 3
Set Sh1 = Sheets(shary(3 * Rnds - 3)): Set Sh2 = Sheets(shary(3 * Rnds - 2)): Set Sh3 = Sheets(shary(3 * Rnds - 1))
Cnt = 0

If Rnds < 3 Then
A = Sh1.Range("A1").CurrentRegion
For T1 = 2 To UBound(A, 1)
temp = A(T1, 2) & "_" & A(T1, 3) & "_" & A(T1, 4) & "_" & A(T1, 5)
Dic(k).Add temp, A(T1, 6)
Dic(k + 1).Add temp, A(T1, 7)
Next T1
End If
Cnt = Dic(k).Count

B = Sh2.Range("A1").CurrentRegion
For T2 = 2 To UBound(B, 1)
S = B(T2, 2) & "_" & B(T2, 3) & "_" & B(T2, 4) & "_" & B(T2, 5)
With Dic(k)
If .exists(S) Then
.Item(S) = .Item(S) - B(T2, 6)
Dic(k + 1).Item(S) = Dic(k + 1).Item(S) - B(T2, 7)
Else
.Add S, B(T2, 6)
Dic(2 * Rnds).Add S, B(T2, 7)

Cnt = Cnt + 1
End If
End With
Next T2

If Cnt > 0 Then
With Sh3
.Cells.Clear
    With .Range("B2").Resize(Cnt, 1)
    .Value = WorksheetFunction.Transpose(Dic(k).keys)
    .TextToColumns Destination:=Range("B2"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
        :="_", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1)), _
        TrailingMinusNumbers:=True
    End With
    .Range("F2").Resize(Cnt, 1).Value = WorksheetFunction.Transpose(Dic(k).items)
    .Range("G2").Resize(Cnt, 1).Value = WorksheetFunction.Transpose(Dic(k + 1).items)
    
    With .Range("A2").Resize(Cnt, 1)
    .Formula = "=row(A1)"
    .Value = .Value
    .Offset(0, 6).NumberFormat = "[$TRY] #,##0.00"
    End With
    
    .Range("A1:G1") = Array("ITEM", "CO-IT", "FOOD", "TT-MMN", "ORT-WW", "QTY", "TOTAL")
    With .Range("A1").CurrentRegion
    .Borders.LineStyle = xlContinuous
    .EntireColumn.AutoFit
    .HorizontalAlignment = xlCenter
    End With
End With
End If

If Rnds = 2 Then
C = Sheets("FRS").Range("A1").CurrentRegion

For T3 = 2 To UBound(B, 1)
S = C(T3, 2) & "_" & C(T3, 3) & "_" & C(T3, 4) & "_" & C(T3, 5)
With Dic(k)
If .exists(S) Then
.Item(S) = .Item(S) + C(T3, 6)
Dic(2 * Rnds).Item(S) = Dic(k + 1).Item(S) + C(T3, 7)
Else
.Add S, C(T3, 6)
Dic(k + 1).Add S, C(T3, 7)
Cnt = Cnt + 1
End If
End With
Next T3
End If


Next Rnds
End Sub
 
Upvote 0
thanks again .
the result in sheet NET PUR, NET SUR are correct as I posted but the sheet FINAL are wrong data
this is what I got
data.xlsm
ABCDEFG
1ITEMCO-ITFOODTT-MMNORT-WWQTYTOTAL
21COR-FF1FRBANANATT400TRY 8,200.00
32COR-FF2FRAPPLELL100TRY 10,000.00
43COR-FF3FRPEARNN10TRY 100.00
54COR-FF4FRBANANAQQ20TRY 400.00
65COR-FF5VEGTOMATOSS12TRY 144.00
76COR-FF6VEGTOMATOAA12TRY 144.00
87COR-FF11FRPEACHTT0TRY 0.00
98COR-FF12FRAPPLELL0TRY 0.00
109COR-FF14FRBANANAQQ0TRY 0.00
1110COR-FF16VEGTOMATOAA0TRY 0.00
FINAL


and should be as in OP
data.xlsm
ABCDEFG
1ITEMCO-ITFOODTT-MMNORT-WWQTYTOTAL
21COR-FF1FRBANANATT500.00TRY 8,400.00
32COR-FF2FRAPPLELL200.00TRY 20,000.00
43COR-FF3FRPEARNN20.00TRY 200.00
54COR-FF4FRBANANAQQ28.00TRY 160.00
65COR-FF5VEGTOMATOSS24.00TRY 288.00
76COR-FF6VEGTOMATOAA20.00TRY 240.00
87COR-FF11FRPEACHTT629.00TRY 9,398.00
98COR-FF12FRAPPLELL255.00TRY 5,865.00
109COR-FF13FRPEARNN8.00TRY 96.00
1110COR-FF14FRBANANAQQ600.00TRY 13,200.00
1211COR-FF16VEGTOMATOAA48.00TRY 264.00
1312COR-FF17VEGTOMATOAA125.00TRY 265.00
1413COR-FF18VEGTOMATOAA226.00TRY 266.00
FINAL


last thing if update data in others sheets by adding new data or change data when run macro again will gives error as in picture in this line
VBA Code:
Dic(k).Add temp, A(T1, 6)
1.PNG
 
Upvote 0
In the file you uploaded I ran the macro working ok.
If it is some other file pl upload that file.. I think there may be duplicate entry in same sheet.
 
Upvote 0
In the file you uploaded I ran the macro working ok.
very strange !!!!
If it is some other file pl upload that file.. I think there may be duplicate entry in same sheet.
no way the same structure data as in OP and the same file , even of that
again the file data.xlsm
by the way the code doesn't work for me any more. just shows error as I mentioned .
 
Upvote 0
In this file you have uploaded now In SR sheet B3:E3 and B6:E6 are having same data. This is what I mean duplicate data in same sheet. Previous file does not have such data.
 
Upvote 0
sorry about duplicate items . you're right , shouldn't be , but the problem about sheet FINAL still doesn't show right data and some data are missed as I mentioned in post#16 in pic1 . may you test the last file uploaded with ignore duplicates ,please?
actually gives as in pic1 in post#16:confused:
 
Upvote 0

Forum statistics

Threads
1,224,793
Messages
6,180,985
Members
453,011
Latest member
Osamu9Dazai

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