Match three columns together across sheet to subtraction based invoice number

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
651
Office Version
  1. 2019
Hello
I have theses data for four sheets
DECREASE.xlsm
ABCDEFGHI
1ITEMDATEINV.NOBRANDTYPEORIGINQTY PRICETOTAL
2115/06/2023BSTR_23448BS 750R16R230JAP4.00500.002,000.00
3215/06/2023BSTR_23448BS 700R16R230JAP2.00400.00800.00
4SUM2,800.00
5115/09/2023BSTR_23449GO 1200R20AZ0026CHI1.00920.00920.00
6215/09/2023BSTR_23449GO 1200R20AZ0083CHI2.001,000.002,000.00
7SUM2,920.00
8115/09/2023BSTR_23450BS 1200R20G580JAP10.001,800.0018,000.00
9215/09/2023BSTR_23450BS 1200R20G580THI10.001,800.0018,000.00
10315/09/2023BSTR_23450BS 1200R20R187THI10.001,800.0018,000.00
11SUM54,000.00
12116/09/2023BSTR_23451BS 215/60R16ER30JAP4.00400.001,600.00
13SUM1,600.00
14116/09/2023BSTR_23452BS 1200R20G580JAP5.001,800.009,000.00
15SUM9,000.00
16116/09/2023BSTR_23453BS 1200R20G580JAP5.001,880.009,400.00
17SUM9,400.00
SV



DECREASE.xlsm
ABCDEFGHI
1ITEMDATEINV.NOBRANDTYPEORIGINQTY PRICETOTAL
2110/06/2023BSJ_23444BS 215/60R16ER30JAP4.00430.001,720.00
3SUM1,720.00
4110/06/2023BSJ_23445GO 1200R20AZ0026CHI2.00955.001,910.00
5SUM1,910.00
6115/09/2023BSJ_23446GO 1200R20AZ0026CHI2.00950.001,900.00
7215/09/2023BSJ_23446GO 1200R21AZ0027CHI3.001,000.003,000.00
8SUM4,900.00
9115/09/2023BSJ_23447BS 1200R20G580JAP1.002,000.002,000.00
10215/09/2023BSJ_23447BS 1200R20G580THI1.002,000.002,000.00
11315/09/2023BSJ_23447BS 1200R20R187THI1.002,000.002,000.00
12SUM6,000.00
SR



DECREASE.xlsm
ABCDEFGHIJ
1ITEMDATEINV.NOBRANDTYPEORIGINQTY PRICETOTALNOTICE
2115/06/2023VSTR_23444BS 750R16R230JAP1.00500.00500.00INV.NO BSTR_23448
3SUM500.00
4115/09/2023VSTR_23445GO 1200R20AZ0083CHI1.001,000.001,000.00INV.NO BSTR_23449
5SUM1,000.00
6115/09/2023VSTR_23446BS 1200R20G580JAP1.001,800.001,800.00INV NO BSTR_23450
7215/09/2023VSTR_23446BS 1200R20G580THI1.001,800.001,800.00INV NO BSTR_23450
8SUM3,600.00
9116/09/2023VSTR_23447BS 215/60R16ER30JAP4.00400.001,600.00INV NO BSTR_23451
10SUM1,600.00
11116/09/2023VSTR_23448BS 1200R20G580JAP1.001,800.001,800.00INV NO BSTR_23452
12SUM1,800.00
13116/09/2023VSTR_23449BS 1200R20G580JAP2.001,880.003,760.00INV NO BSTR_23453
14SUM3,760.00
VS



DECREASE.xlsm
ABCDEFGHIJ
2110/06/2023RSS_23222BS 215/60R16ER30JAP2.00430.00860.00INV.NO BSJ_23444
3SUM860.00
4110/06/2023BSJ_23445GO 1200R20AZ0026CHI1.00955.00955.00INV.NO BSJ_23445
5SUM955.00
6115/09/2023BSJ_23446GO 1200R20AZ0026CHI1.00950.00950.00INV.NO BSJ_23446
7215/09/2023BSJ_23446GO 1200R21AZ0027CHI2.001,000.002,000.00INV.NO BSJ_23446
8SUM2,950.00
RS



what I want matching columns D:F in SV sheet with columns D:F in VS sheet based on invoice number is in column(J)
if the invoice number in column J in s VS sheet is the same invoice number in column(C) in SV sheet then should subtract QTY for ID in columns D:F in SV sheet from VS sheet and change calculation for TOTAL column for each ID and SUM row for whole invoice number and should put word DONE in column J for adjacant ID cells to avoid subtraction every time repeatedly(meaning when there is DONE word shouldn't do any thing). and if there is the same ID for the same QTY then should delete the whole row for ID like 215/60R16 ER30 and whole row for SUM row if there is no another ID for the same invoice number .as to PRICE column will not change because will be the same

so in result in SV sheet should be as highlighted cells
DECREASE.xlsm
ABCDEFGHIJ
1ITEMDATEINV.NOBRANDTYPEORIGINQTY PRICETOTAL
2115/06/2023BSTR_23448BS 750R16R230JAP3.00500.001,500.00DONE
3215/06/2023BSTR_23448BS 700R16R230JAP2.00400.00800.00DONE
4SUM2,300.00
5115/09/2023BSTR_23449GO 1200R20AZ0026CHI1.00920.00920.00
6215/09/2023BSTR_23449GO 1200R20AZ0083CHI1.001,000.001,000.00DONE
7SUM1,920.00
8115/09/2023BSTR_23450BS 1200R20G580JAP9.001,800.0016,200.00DONE
9215/09/2023BSTR_23450BS 1200R20G580THI9.001,800.0016,200.00DONE
10315/09/2023BSTR_23450BS 1200R20R187THI10.001,800.0018,000.00
11SUM50,400.00
12116/09/2023BSTR_23452BS 1200R20G580JAP4.001,800.007,200.00DONE
13SUM7,200.00
14116/09/2023BSTR_23453BS 1200R20G580JAP3.001,880.005,640.00DONE
15SUM5,640.00
SV


also result in SR sheet bases on RS sheet with the same way as I did it in SV sheet.
expected data in SV,SR sheets contain 8500 rows .
thanks
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
If you had more than 2 pairs of sheets, you can place them in this line of the macro:

VBA Code:
ary = Array("SV", "VS", "SR", "RS")

Try this:
VBA Code:
Sub match_columns()
  'Match three columns together across sheet to subtraction based invoice number
  Dim dic As Object
  Dim a() As Variant, b() As Variant, c() As Variant, ary As Variant, ky As Variant, invNo As Variant
  Dim i As Long, j As Long, k As Long, m As Long, nRow As Long
  
  ary = Array("SV", "VS", "SR", "RS")
  
  For k = 0 To UBound(ary) Step 2
    Erase a, b
    Set dic = CreateObject("Scripting.Dictionary")
    a = Sheets(ary(k)).Range("A1:J" & Sheets(ary(k)).Range("A" & Rows.Count).End(3).Row).Value
    b = Sheets(ary(k + 1)).Range("A1:J" & Sheets(ary(k + 1)).Range("A" & Rows.Count).End(3).Row).Value
    ReDim c(1 To UBound(a, 1), 1 To UBound(a, 2))
    
    For i = 2 To UBound(a)
      ky = a(i, 3) & "|" & a(i, 4) & "|" & a(i, 5) & "|" & a(i, 6)
      dic(ky) = i
    Next
    
    For i = 2 To UBound(b)
      If InStr(1, b(i, 10), " ") > 0 Then
        invNo = Split(b(i, 10), " ")
        ky = Trim(invNo(UBound(invNo))) & "|" & b(i, 4) & "|" & b(i, 5) & "|" & b(i, 6)
        If dic.exists(ky) Then
          nRow = dic(ky)
          a(nRow, 7) = a(nRow, 7) - b(i, 7)
          a(nRow, 9) = a(nRow, 9) - b(i, 9)
          a(nRow, 10) = "DONE"
          For j = nRow + 1 To UBound(a)
            If a(j, 1) = "SUM" Then
              a(j, 9) = a(j, 9) - b(i, 9)
              Exit For
            End If
          Next
        End If
      End If
    Next
    
    m = 0
    For i = 1 To UBound(a, 1)
      If a(i, 9) <> 0 Then
        m = m + 1
        For j = 1 To UBound(a, 2)
          c(m, j) = a(i, j)
        Next
      End If
    Next
    
    With Sheets(ary(k))
      .Range("A2").Resize(UBound(c, 1)).Interior.ColorIndex = xlColorIndexNone
      .Range("A1").Resize(UBound(c, 1), UBound(c, 2)).Value = c
      Application.ReplaceFormat.Clear
      Application.ReplaceFormat.Interior.Color = 8696052
      .Range("A:A").Replace "SUM", "SUM", xlPart, xlByRows, False, False, ReplaceFormat:=True
      Application.ReplaceFormat.Clear
    End With
  Next
End Sub

🤗
 
Upvote 0
awesome!
but I need fixing subtraction repeatedly when run every time the macro.
should put word DONE in column J for adjacant ID cells to avoid subtraction every time repeatedly(meaning when there is DONE word shouldn't do any thing)
doesn't seem to be clear for you .
ok I meant when there is DONE word for each ID after run the macro from first time and try run the macro every time for just DONE word then stop subtraction to avoid subtraction every time repeatedly and gives wrong calculation .
also in ITEM for column A and when delete entire row for ID contains the same QTY and delete it ,then should re-auto-numbering before SUM row like 1,2 instead of keep old number in column A .
thanks.
 
Upvote 0
but I need fixing subtraction repeatedly when run every time the macro.
I forgot that part.

Try this:

VBA Code:
Sub match_columns()
  'Match three columns together across sheet to subtraction based invoice number
  Dim dic As Object
  Dim a() As Variant, b() As Variant, c() As Variant, ary As Variant, ky As Variant, invNo As Variant
  Dim i As Long, j As Long, k As Long, m As Long, n As Long, nRow As Long
  
  ary = Array("SV", "VS", "SR", "RS")
  
  For k = 0 To UBound(ary) Step 2
    Erase a, b
    Set dic = CreateObject("Scripting.Dictionary")
    a = Sheets(ary(k)).Range("A1:J" & Sheets(ary(k)).Range("A" & Rows.Count).End(3).Row).Value
    b = Sheets(ary(k + 1)).Range("A1:J" & Sheets(ary(k + 1)).Range("A" & Rows.Count).End(3).Row).Value
    ReDim c(1 To UBound(a, 1), 1 To UBound(a, 2))
    
    For i = 2 To UBound(a)
      If a(i, 10) <> "DONE" Then
        ky = a(i, 3) & "|" & a(i, 4) & "|" & a(i, 5) & "|" & a(i, 6)
        dic(ky) = i
      End If
    Next
    
    For i = 2 To UBound(b)
      If InStr(1, b(i, 10), " ") > 0 Then
        invNo = Split(b(i, 10), " ")
        ky = Trim(invNo(UBound(invNo))) & "|" & b(i, 4) & "|" & b(i, 5) & "|" & b(i, 6)
        If dic.exists(ky) Then
          nRow = dic(ky)
          a(nRow, 7) = a(nRow, 7) - b(i, 7)
          a(nRow, 9) = a(nRow, 9) - b(i, 9)
          a(nRow, 10) = "DONE"
          For j = nRow + 1 To UBound(a)
            If a(j, 1) = "SUM" Then
              a(j, 9) = a(j, 9) - b(i, 9)
              Exit For
            End If
          Next
        End If
      End If
    Next
    
    m = 0
    n = 0
    For i = 1 To UBound(a, 1)
      If a(i, 9) <> 0 Then
        m = m + 1
        n = n + 1
        If a(i, 1) = "ITEM" Or a(i, 1) = "SUM" Then
          c(m, 1) = a(i, 1)
          n = 0
        Else
          c(m, 1) = n
        End If
        
        For j = 2 To UBound(a, 2)
          c(m, j) = a(i, j)
        Next
      End If
    Next
    
    With Sheets(ary(k))
      .Range("A2").Resize(UBound(c, 1)).Interior.ColorIndex = xlColorIndexNone
      .Range("A1").Resize(UBound(c, 1), UBound(c, 2)).Value = c
      Application.ReplaceFormat.Clear
      Application.ReplaceFormat.Interior.Color = 8696052
      .Range("A:A").Replace "SUM", "SUM", xlPart, xlByRows, False, False, ReplaceFormat:=True
      Application.ReplaceFormat.Clear
    End With
  Next
End Sub


😇
 
Upvote 0
Perfect !
I have strange case !
sometimes when delete some row for ID will change formatting in font as in row 11,13
Abdoda.xlsm
ABCDEFGHIJ
1ITEMDATEINV.NOBRANDTYPEORIGINQTY PRICETOTAL
2115/06/2023BSTR_23448BS 750R16R230JAP3.00500.001,500.00DONE
3215/06/2023BSTR_23448BS 700R16R230JAP2.00400.00800.00
4SUM2,300.00
5115/09/2023BSTR_23449GO 1200R20AZ0026CHI1.00920.00920.00
6215/09/2023BSTR_23449GO 1200R20AZ0083CHI1.001,000.001,000.00DONE
7SUM1,920.00
8115/09/2023BSTR_23450BS 1200R20G580THI9.001,800.0016,200.00DONE
9215/09/2023BSTR_23450BS 1200R20R187THI9.001,800.0016,200.00DONE
10SUM32,400.00
11116/09/2023BSTR_23452BS 1200R20G580JAP4.001,800.007,200.00DONE
12SUM7,200.00
13116/09/2023BSTR_23453BS 1200R20G580JAP3.001,880.005,640.00DONE
14SUM5,640.00
SV

do you have any idea why do that?
 
Upvote 0
sometimes when delete some row for ID will change formatting in font as in row 11,13


do you have any idea why do that?

Those rows (11, 13) before the macro had a "SUM" row, so that is the format of rows 11 and 13.

Recommendation: Put the format you want in all the columns, since the macro does not remove or add formats to the columns, except for column A, since the "SUM" can move.

;)
 
Upvote 0
I can't download files.

As I told you, before running the macro, set the format you want for the entire column for each column.

And try again the macro.
 
Upvote 0
What is the problem?
the same thing
DECREASE.xlsm
ABCDEFGHIJ
1ITEMDATEINV.NOBRANDTYPEORIGINQTY PRICETOTAL
2115/06/2023BSTR_23448BS 750R16R230JAP2.00500.001,000.00DONE
3215/06/2023BSTR_23448BS 700R16R230JAP2.00400.00800.00
4SUM1,800.00
5115/09/2023BSTR_23449GO 1200R20AZ0026CHI1.00920.00920.00
6215/09/2023BSTR_23449GO 1200R20AZ0083CHI1.001,000.001,000.00
7SUM1,920.00
8115/09/2023BSTR_23450BS 1200R20G580JAP9.001,800.0016,200.00
9215/09/2023BSTR_23450BS 1200R20R187THI10.001,800.0018,000.00
10SUM34,200.00
11116/09/2023BSTR_23452BS 1200R20G580JAP3.001,800.005,400.00DONE
12SUM5,400.00
13116/09/2023BSTR_23453BS 1200R20G580JAP1.001,880.001,880.00DONE
14SUM1,880.00
15116/09/2023BSTR_23455BS 1200R20G580JAP3.001,900.005,700.00
16SUM5,700.00
17
SV


If it is a format, as I told you, before running the macro, set the format you want for the entire column for each column.
yes I did it by select all of columns for all of sheets and set the formatting ,that's why I attached file.
 
Upvote 0
I added the formatting setting, in this part, Just set the name of the font you want.

Rich (BB code):
      'format cells
      With .Range("A:J")
        .HorizontalAlignment = xlCenter
        .Font.Size = 14
        .Font.Name = "Arial"        'set your favorite font name
      End With
      .Range("G:I").NumberFormat = "#,##0.00"

Try:
VBA Code:
Sub match_columns()
  'Match three columns together across sheet to subtraction based invoice number
  Dim dic As Object
  Dim a() As Variant, b() As Variant, c() As Variant, ary As Variant, ky As Variant, invNo As Variant
  Dim i As Long, j As Long, k As Long, m As Long, n As Long, nRow As Long
  Application.ScreenUpdating = False
  
  ary = Array("SV", "VS", "SR", "RS")
  
  For k = 0 To UBound(ary) Step 2
    Erase a, b
    Set dic = CreateObject("Scripting.Dictionary")
    a = Sheets(ary(k)).Range("A1:J" & Sheets(ary(k)).Range("A" & Rows.Count).End(3).Row).Value
    b = Sheets(ary(k + 1)).Range("A1:J" & Sheets(ary(k + 1)).Range("A" & Rows.Count).End(3).Row).Value
    ReDim c(1 To UBound(a, 1), 1 To UBound(a, 2))
    
    For i = 2 To UBound(a)
      If a(i, 10) <> "DONE" Then
        ky = a(i, 3) & "|" & a(i, 4) & "|" & a(i, 5) & "|" & a(i, 6)
        dic(ky) = i
      End If
    Next
    
    For i = 2 To UBound(b)
      If InStr(1, b(i, 10), " ") > 0 Then
        invNo = Split(b(i, 10), " ")
        ky = Trim(invNo(UBound(invNo))) & "|" & b(i, 4) & "|" & b(i, 5) & "|" & b(i, 6)
        If dic.exists(ky) Then
          nRow = dic(ky)
          a(nRow, 7) = a(nRow, 7) - b(i, 7)
          a(nRow, 9) = a(nRow, 9) - b(i, 9)
          a(nRow, 10) = "DONE"
          For j = nRow + 1 To UBound(a)
            If a(j, 1) = "SUM" Then
              a(j, 9) = a(j, 9) - b(i, 9)
              Exit For
            End If
          Next
        End If
      End If
    Next
    
    m = 0
    n = 0
    For i = 1 To UBound(a, 1)
      If a(i, 9) <> 0 Then
        m = m + 1
        n = n + 1
        If a(i, 1) = "ITEM" Or a(i, 1) = "SUM" Then
          c(m, 1) = a(i, 1)
          n = 0
        Else
          c(m, 1) = n
        End If
        
        For j = 2 To UBound(a, 2)
          c(m, j) = a(i, j)
        Next
      End If
    Next
    
    With Sheets(ary(k))
      .Range("A2").Resize(UBound(c, 1)).Interior.ColorIndex = xlColorIndexNone
      .Range("A1").Resize(UBound(c, 1), UBound(c, 2)).Value = c
      
      'format cells
      With .Range("A:J")
        .HorizontalAlignment = xlCenter
        .Font.Size = 14
        .Font.Name = "Arial"        'set your favorite font name
      End With
      .Range("G:I").NumberFormat = "#,##0.00"
      
      Application.ReplaceFormat.Clear
      Application.ReplaceFormat.Interior.Color = 8696052
      .Range("A:A").Replace "SUM", "SUM", xlPart, xlByRows, False, False, ReplaceFormat:=True
      Application.ReplaceFormat.Clear
      .Range("K:L").EntireColumn.AutoFit
      'end format cells
    End With
    
  Next
  Application.ScreenUpdating = True
End Sub


😇
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,175
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