Macro to extract repeating invoice numbers and details

Omar M

Board Regular
Joined
Jan 11, 2024
Messages
66
Office Version
  1. 2019
Platform
  1. Windows
Hi ,
I search for macro to merge data in OUT sheet for each INVOICE NO for each sheet .
should merge amounts in TOTAL column (H) based on duplicates INVOICE NO in column C ,CONDITION in column D for each sheet alone and exclude columns E:G from merging .
the result should be in OUT sheet from row2 and should show sheet name for each sheet in column E where the INVOICE NO is existed.
this is small data in sample but I would deal with 9500 rows for each sheet.

INVV.xlsm
ABCDEFGH
1ITEM DATEINVOICE NO CONDITIONIDQTYUNIT PRICETOTAL
2101/11/2023SSLL-001PAIDCSDFA122.00122.002,684.00
3201/11/2023SSLL-001PAIDCSDFA211.00125.001,375.00
4301/11/2023SSLL-001PAIDCSDFA3456.00123.0056,088.00
5404/11/2023SSLL-002NOT PAIDCSDFA466.00144.009,504.00
6505/11/2023SSLL-003NOT PAIDCSDFA555.00133.007,315.00
7606/11/2023SSLL-004NOT PAIDCSDFA6222.00144.0031,968.00
8706/11/2023SSLL-004NOT PAIDCSDFA745.00112.005,040.00
9806/11/2023SSLL-004NOT PAIDCSDFA8133.00122.0016,226.00
10909/11/2023SSLL-005NOT PAIDCSDFA934.00109.003,706.00
111010/11/2023SSLL-006PAIDCSDFA1012.00122.001,464.00
121110/11/2023SSLL-006PAIDCSDFA1112.00120.001,440.00
131212/11/2023SSLL-007CASH BY SAFECSDFA1112.00120.001,440.00
141312/11/2023SSLL-007CASH BY SAFECSDFA1212.00120.001,440.00
151413/11/2023SSLL-008CASH BY BANKCSDFA8133.00122.0016,226.00
161513/11/2023SSLL-008CASH BY BANKCSDFA9133.00122.0016,226.00
SL
Cell Formulas
RangeFormula
H2:H16H2=F2*G2


INVV.xlsm
ABCDEFGH
1ITEM DATEINVOICE NO CONDITIONIDQTYUNIT PRICETOTAL
2115/01/2023MMLLL-001PAIDCSDFA92.00110.00220.00
3215/01/2023MMLLL-001PAIDCSDFA1011.00102.001,122.00
4315/01/2023MMLLL-002PAIDCSDFA36.00121.00726.00
5415/01/2023MMLLL-002PAIDCSDFA46.00141.00846.00
6515/01/2023MMLLL-002PAIDCSDFA55.00102.00510.00
7616/01/2023MMLLL-003NOT PAIDCSDFA611.00122.001,342.00
8706/11/2023MMLLL-003NOT PAIDCSDFA710.00100.001,000.00
9806/11/2023MMLLL-003NOT PAIDCSDFA813.00112.001,456.00
10906/11/2023MMLLL-004CASH BY BANKCSDFA923.00100.002,300.00
111006/11/2023MMLLL-004CASH BY BANKCSDFA1010.0090.00900.00
121107/11/2023MMLLL-005CASH BY SAFECSDFA55.00102.00510.00
131207/11/2023MMLLL-005CASH BY SAFECSDFA55.00102.00510.00
ML
Cell Formulas
RangeFormula
H2:H13H2=F2*G2


INVV.xlsm
ABCDEFGH
1ITEM DATEINVOICE NO CONDITIONIDQTYUNIT PRICETOTAL
2117/11/2023TTLL-001CASH BY BANKCSDFA622.00160.003,520.00
3217/11/2023TTLL-001CASH BY BANKCSDFA722.00130.002,860.00
4317/11/2023TTLL-001CASH BY BANKCSDFA344.00140.006,160.00
5417/11/2023TTLL-002NOT PAIDCSDFA610.00160.001,600.00
6517/11/2023TTLL-002NOT PAIDCSDFA710.00130.001,300.00
7618/11/2023TTLL-003NOT PAIDCSDFA610.00150.001,500.00
8718/11/2023TTLL-004PAIDCSDFA722.00128.002,816.00
9818/11/2023TTLL-005PAIDCSDFA8100.00150.0015,000.00
10918/11/2023TTLL-006CASH BY SAFECSDFA930.00140.004,200.00
111018/11/2023TTLL-006CASH BY SAFECSDFA1035.00150.005,250.00
TL
Cell Formulas
RangeFormula
H2:H11H2=F2*G2


INVV.xlsm
ABCDEF
1ITEM DATEINVOICE NO CONDITIONINVOICE TYPTOTAL
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
OUT



result after

INVV.xlsm
ABCDEF
1ITEM DATEINVOICE NO CONDITIONINVOICE TYPTOTAL
2101/11/2023SSLL-001PAIDSL60,147.00
3204/11/2023SSLL-002NOT PAIDSL9,504.00
4305/11/2023SSLL-003NOT PAIDSL7,315.00
5406/11/2023SSLL-004NOT PAIDSL53,234.00
6509/11/2023SSLL-005NOT PAIDSL3,706.00
7610/11/2023SSLL-006PAIDSL2,904.00
8712/11/2023SSLL-007CASH BY SAFESL2,880.00
9813/11/2023SSLL-008CASH BY BANKSL32,452.00
10915/01/2023MMLLL-001PAIDML1,342.00
111015/01/2023MMLLL-002PAIDML2,082.00
121116/01/2023MMLLL-003NOT PAIDML3,798.00
131206/11/2023MMLLL-004CASH BY BANKML3,200.00
141307/11/2023MMLLL-005CASH BY SAFEML1,020.00
151417/11/2023TTLL-001CASH BY BANKTL12,540.00
161517/11/2023TTLL-002NOT PAIDTL2,900.00
171618/11/2023TTLL-003NOT PAIDTL1,500.00
181718/11/2023TTLL-004PAIDTL2,816.00
191818/11/2023TTLL-005PAIDTL15,000.00
201918/11/2023TTLL-006CASH BY SAFETL9,450.00
OUT



I hope to find solution soon.
thanks for everyone see my subject.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try this:

VBA Code:
Sub MergeData()
  Dim shs As Variant, a() As Variant, b() As Variant, s As Variant
  Dim dic As Object, ky As String
  Dim i As Long, n As Long, y As Long
  
  shs = Array("SL", "ML", "TL")
  Set dic = CreateObject("SCripting.Dictionary")
  
  For Each s In shs
    Erase a, b
    a = Sheets(s).Range("A2", Sheets(s).Range("H" & Rows.Count).End(3)).Value
    ReDim b(1 To UBound(a, 1), 1 To 6)
    dic.RemoveAll
    y = 0
    
    For i = 1 To UBound(a, 1)
      ky = a(i, 3) & "|" & a(i, 4)
      If Not dic.exists(ky) Then
        y = y + 1
        dic(ky) = y
        n = n + 1
      End If
      y = dic(ky)
      b(y, 1) = n
      b(y, 2) = a(i, 2)
      b(y, 3) = a(i, 3)
      b(y, 4) = a(i, 4)
      b(y, 5) = s
      b(y, 6) = b(y, 6) + a(i, 8)
    Next
    Sheets("OUT").Range("A" & Rows.Count).End(3)(2).Resize(UBound(b, 1), UBound(b, 2)).Value = b
  Next
End Sub
 
Upvote 0
Solution
Awesome !
may you adapt the code by clear data in OUT sheet before brings data.
actually I don't repeat copying the same data to the bottom when run the macro every time.
thanks.
 
Upvote 0
may you adapt the code by clear data in OUT sheet before brings data.
After this line:
VBA Code:
  Set dic = CreateObject("SCripting.Dictionary")

Put this line:
VBA Code:
  Sheets("OUT").Range("A2:F" & Rows.Count).ClearContents
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,138
Members
453,021
Latest member
Justyna P

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