Compare two sheets based on lastrow with highlighted cells

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
651
Office Version
  1. 2019
Hi,
I have two sheets contains data and every time the last row will change in location so what I want compare amount in lastrow for column F in INVOICE sheet with amount in lastrow for column E in ACCOUNT sheet .if the amount in last row in ACCOUNT sheet bigger than amount in last row in INVOICE sheet then will highlighted by red and write the difference for adjacent cell by plus or deficit and amount , and if it's matched the amounts between two sheets then will highlighted yellow and write matched .
will show result for both sheets in last amount in last column .
example:
COMPARE.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221VEGA 70A R KOR4.00500.002,000.00
232VEGA 55A R KOR2.00425.00850.00
243LARGEST 60A L HIGH JAP2.00440.00880.00
254KM 215/65R16 TA31 KOR4.00460.001,840.00
265265/70R16 ALGERIA4.00630.002,520.00
276KM 205/65R16 HS63 KOR4.00445.001,780.00
287GC 385/65R22.5 AT131 CHI2.001,650.003,300.00
298KM 265/70R16 KOR4.00880.003,520.00
309DONGA 66A L KOR2.00460.00920.00
3110KM 235/65R17 HP71 KOR4.00625.002,500.00
3211KM 235/55R19 PS71 KOR4.00675.002,700.00
3312DROUB 90A L KOR1.00575.00575.00
3413KM 13R22.5 MA11 KOR2.002,550.005,100.00
35TOTAL39.0028,485.00
INVOICE
Cell Formulas
RangeFormula
D35,F35D35=SUM(D22:D34)
F22:F34F22=D22*E22



COMPARE.xlsm
ABCDE
1DATEINFODEBITCREDITBALANCE
201/11/2024SALES INVOICE NO BSJ20002,000.002,000.00
302/11/2024SALES INVOICE NO BSJ20013,000.005,000.00
403/11/2024SALES RETURNS INVOICE NO BSJ7893,000.002,000.00
503/11/2024VOUCHER NO VBGH67881,500.00500.00
604/11/2024SALES INVOICE NO BSJ200315,000.0015,500.00
704/11/2024SALES INVOICE NO BSJ200413,000.0028,500.00
8
9
10
11
12
13
14
15
ACCOUNT
Cell Formulas
RangeFormula
E2E2=C2-D2
E3:E7E3=E2+C3-D3



result

COMPARE.xlsm
ABCDEF
1DATEINFODEBITCREDITBALANCE
201/11/2024SALES INVOICE NO BSJ20002,000.002,000.00
302/11/2024SALES INVOICE NO BSJ20013,000.005,000.00
403/11/2024SALES RETURNS INVOICE NO BSJ7893,000.002,000.00
503/11/2024VOUCHER NO VBGH67881,500.00500.00
604/11/2024SALES INVOICE NO BSJ200315,000.0015,500.00
704/11/2024SALES INVOICE NO BSJ200413,000.0028,500.00PLUS 15.00
8
9
10
11
12
13
14
15
ACCOUNT
Cell Formulas
RangeFormula
E2E2=C2-D2
E3:E7E3=E2+C3-D3




COMPARE.xlsm
BCDEFG
21ITEMBRANDQTYPRICEBALANCE
221VEGA 70A R KOR4.00500.002,000.00
232VEGA 55A R KOR2.00425.00850.00
243LARGEST 60A L HIGH JAP2.00440.00880.00
254KM 215/65R16 TA31 KOR4.00460.001,840.00
265265/70R16 ALGERIA4.00630.002,520.00
276KM 205/65R16 HS63 KOR4.00445.001,780.00
287GC 385/65R22.5 AT131 CHI2.001,650.003,300.00
298KM 265/70R16 KOR4.00880.003,520.00
309DONGA 66A L KOR2.00460.00920.00
3110KM 235/65R17 HP71 KOR4.00625.002,500.00
3211KM 235/55R19 PS71 KOR4.00675.002,700.00
3312DROUB 90A L KOR1.00575.00575.00
3413KM 13R22.5 MA11 KOR2.002,550.005,100.00
35TOTAL39.0028,485.00DEFICIT -15.00
INVOICE
Cell Formulas
RangeFormula
D35,F35D35=SUM(D22:D34)
F22:F34F22=D22*E22



another example

COMPARE.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221VEGA 70A R KOR4.00500.002,000.00
232VEGA 55A R KOR2.00425.00850.00
243LARGEST 60A L HIGH JAP2.00440.00880.00
254KM 215/65R16 TA31 KOR4.00460.001,840.00
265265/70R16 ALGERIA4.00630.002,520.00
276KM 205/65R16 HS63 KOR4.00445.001,780.00
287GC 385/65R22.5 AT131 CHI2.001,650.003,300.00
298KM 265/70R16 KOR4.00880.003,520.00
309DONGA 66A L KOR2.00460.00920.00
3110KM 235/65R17 HP71 KOR4.00625.002,500.00
3211KM 235/55R19 PS71 KOR4.00675.002,700.00
3312XCV 90A L KOR1.00580.00580.00
3413BJS 13R22.5 MA11 KOR2.002,555.005,110.00
3514KM 13R22.5 MA11 KOR5.002,000.0010,000.00
36TOTAL44.0038,500.00
INVOICE
Cell Formulas
RangeFormula
D36,F36D36=SUM(D22:D35)
F22:F35F22=D22*E22


COMPARE.xlsm
ABCDEF
1DATEINFODEBITCREDITBALANCE
201/11/2024SALES INVOICE NO BSJ20002,000.002,000.00
302/11/2024SALES INVOICE NO BSJ20013,000.005,000.00
403/11/2024SALES RETURNS INVOICE NO BSJ7893,000.002,000.00
503/11/2024VOUCHER NO VBGH67881,500.00500.00
604/11/2024SALES INVOICE NO BSJ200315,000.0015,500.00
704/11/2024SALES INVOICE NO BSJ200413,000.0028,500.00PLUS 15.00
805/11/2024SALES INVOICE NO BSJ200515,000.0043,500.00
905/11/2024SALES INVOICE NO BSJ20061,000.0044,500.00
1005/11/2024VOUCHER NO VBGH67891,000.0043,500.00
1105/11/2024VOUCHER NO VBGH67905,000.0038,500.00
12
13
14
15
ACCOUNT
Cell Formulas
RangeFormula
E2E2=C2-D2
E3:E11E3=E2+C3-D3



result
COMPARE.xlsm
BCDEFG
21ITEMBRANDQTYPRICEBALANCE
221VEGA 70A R KOR4.00500.002,000.00
232VEGA 55A R KOR2.00425.00850.00
243LARGEST 60A L HIGH JAP2.00440.00880.00
254KM 215/65R16 TA31 KOR4.00460.001,840.00
265265/70R16 ALGERIA4.00630.002,520.00
276KM 205/65R16 HS63 KOR4.00445.001,780.00
287GC 385/65R22.5 AT131 CHI2.001,650.003,300.00
298KM 265/70R16 KOR4.00880.003,520.00
309DONGA 66A L KOR2.00460.00920.00
3110KM 235/65R17 HP71 KOR4.00625.002,500.00
3211KM 235/55R19 PS71 KOR4.00675.002,700.00
3312XCV 90A L KOR1.00580.00580.00
3413BJS 13R22.5 MA11 KOR2.002,555.005,110.00
3514KM 13R22.5 MA11 KOR5.002,000.0010,000.00
36TOTAL44.0038,500.00MATCHED
INVOICE
Cell Formulas
RangeFormula
D36,F36D36=SUM(D22:D35)
F22:F35F22=D22*E22



COMPARE.xlsm
ABCDEF
1DATEINFODEBITCREDITBALANCE
201/11/2024SALES INVOICE NO BSJ20002,000.002,000.00
302/11/2024SALES INVOICE NO BSJ20013,000.005,000.00
403/11/2024SALES RETURNS INVOICE NO BSJ7893,000.002,000.00
503/11/2024VOUCHER NO VBGH67881,500.00500.00
604/11/2024SALES INVOICE NO BSJ200315,000.0015,500.00
704/11/2024SALES INVOICE NO BSJ200413,000.0028,500.00PLUS 15.00
805/11/2024SALES INVOICE NO BSJ200515,000.0043,500.00
905/11/2024SALES INVOICE NO BSJ20061,000.0044,500.00
1005/11/2024VOUCHER NO VBGH67891,000.0043,500.00
1105/11/2024VOUCHER NO VBGH67905,000.0038,500.00MATCHED
12
13
14
15
ACCOUNT
Cell Formulas
RangeFormula
E2E2=C2-D2
E3:E11E3=E2+C3-D3


Last example
COMPARE.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221BJS 13R22.5 MA11 KOR10.002,555.0025,550.00
232KM 13R22.5 MA11 KOR12.002,000.0024,000.00
24TOTAL22.0049,550.00
INVOICE
Cell Formulas
RangeFormula
D24,F24D24=SUM(D22:D23)
F22:F23F22=D22*E22


COMPARE.xlsm
ABCDEF
1DATEINFODEBITCREDITBALANCE
201/11/2024SALES INVOICE NO BSJ20002,000.002,000.00
302/11/2024SALES INVOICE NO BSJ20013,000.005,000.00
403/11/2024SALES RETURNS INVOICE NO BSJ7893,000.002,000.00
503/11/2024VOUCHER NO VBGH67881,500.00500.00
604/11/2024SALES INVOICE NO BSJ200315,000.0015,500.00
704/11/2024SALES INVOICE NO BSJ200413,000.0028,500.00PLUS 15.00
805/11/2024SALES INVOICE NO BSJ200515,000.0043,500.00
905/11/2024SALES INVOICE NO BSJ20061,000.0044,500.00
1005/11/2024VOUCHER NO VBGH67891,000.0043,500.00
1105/11/2024VOUCHER NO VBGH67905,000.0038,500.00MATCHED
1205/11/2024SALES INVOICE NO BSJ20072,000.0040,500.00
1305/11/2024SALES INVOICE NO BSJ20083,000.0043,500.00
1405/11/2024SALES INVOICE NO BSJ20095,000.0048,500.00
15
ACCOUNT
Cell Formulas
RangeFormula
E2E2=C2-D2
E3:E14E3=E2+C3-D3



result

COMPARE.xlsm
BCDEFG
21ITEMBRANDQTYPRICEBALANCE
221BJS 13R22.5 MA11 KOR10.002,555.0025,550.00
232KM 13R22.5 MA11 KOR12.002,000.0024,000.00
24TOTAL22.0049,550.00PLUS 1,050.00
INVOICE
Cell Formulas
RangeFormula
D24,F24D24=SUM(D22:D23)
F22:F23F22=D22*E22



COMPARE.xlsm
ABCDEFG
1DATEINFODEBITCREDITBALANCE
201/11/2024SALES INVOICE NO BSJ20002,000.002,000.00
302/11/2024SALES INVOICE NO BSJ20013,000.005,000.00
403/11/2024SALES RETURNS INVOICE NO BSJ7893,000.002,000.00
503/11/2024VOUCHER NO VBGH67881,500.00500.00
604/11/2024SALES INVOICE NO BSJ200315,000.0015,500.00
704/11/2024SALES INVOICE NO BSJ200413,000.0028,500.00PLUS 15.00
805/11/2024SALES INVOICE NO BSJ200515,000.0043,500.00
905/11/2024SALES INVOICE NO BSJ20061,000.0044,500.00
1005/11/2024VOUCHER NO VBGH67891,000.0043,500.00
1105/11/2024VOUCHER NO VBGH67905,000.0038,500.00MATCHED
1205/11/2024SALES INVOICE NO BSJ20072,000.0040,500.00
1305/11/2024SALES INVOICE NO BSJ20083,000.0043,500.00
1405/11/2024SALES INVOICE NO BSJ20095,000.0048,500.00DEFICIT -1,050.00
15
ACCOUNT
Cell Formulas
RangeFormula
E2E2=C2-D2
E3:E14E3=E2+C3-D3
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try:
VBA Code:
Sub CompareValues()
    Application.ScreenUpdating = False
    Dim invWS As worsheeet, accWS As Worksheet, lRowInv As Long, lRowAcc As Long
    Set invWS = Sheets("INVOICE")
    Set accWS = Sheets("ACCOUNT")
    lRowInv = invWS.Range("F" & Rows.Count).End(xlUp).Row
    lRowAcc = accWS.Range("E" & Rows.Count).End(xlUp).Row
    If accWS.Range("E" & lRowAcc).Value > invWS.Range("F" & lRowInv).Value Then
        accWS.Range("E" & lRowAcc).Interior.ColorIndex = vbRed
        accWS.Range("F" & lRowAcc) = "PLUS " & accWS.Range("E" & lRowAcc).Value - invWS.Range("F" & lRowInv).Value
        invWS.Range("G" & lRowInv).Value = "DEFICIT " & invWS.Range("F" & lRowInv).Value - accWS.Range("E" & lRowAcc).Value
    ElseIf accWS.Range("E" & lRowInv).Value = invWS.Range("F" & lRowAcc).Value Then
        accWS.Range("E" & lRowAcc).Interior.ColorIndex = vbYellow
        accWS.Range("F" & lRowAcc) = "MATCHED "
        invWS.Range("F" & lRowInv).Interior.ColorIndex = vbYellow
        invWS.Range("G" & lRowInv) = "MATCHED"
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi,
I'm not sure why show subscript out of range in this line
VBA Code:
 accWS.Range("E" & lRowAcc).Interior.ColorIndex = vbRed
by the way you have typo for declarer sheet
Rich (BB code):
 Dim invWS As worsheeet
should be worksheet.
 
Upvote 0
Try:
VBA Code:
Sub CompareValues()
    Application.ScreenUpdating = False
    Dim invWS As Worksheet, accWS As Worksheet, lRowInv As Long, lRowAcc As Long
    Set invWS = Sheets("INVOICE")
    Set accWS = Sheets("ACCOUNT")
    lRowInv = invWS.Range("F" & Rows.Count).End(xlUp).Row
    lRowAcc = accWS.Range("E" & Rows.Count).End(xlUp).Row
    If accWS.Range("E" & lRowAcc).Value > invWS.Range("F" & lRowInv).Value Then
        accWS.Range("E" & lRowAcc).Interior.ColorIndex = 3
        accWS.Range("F" & lRowAcc) = "PLUS " & accWS.Range("E" & lRowAcc).Value - invWS.Range("F" & lRowInv).Value
        invWS.Range("F" & lRowInv).Interior.ColorIndex = 3
        invWS.Range("G" & lRowInv).Value = "DEFICIT " & invWS.Range("F" & lRowInv).Value - accWS.Range("E" & lRowAcc).Value     
    ElseIf accWS.Range("E" & lRowAcc).Value = invWS.Range("F" & lRowInv).Value Then
        accWS.Range("E" & lRowAcc).Interior.ColorIndex = 6
        accWS.Range("F" & lRowAcc) = "MATCHED "
        invWS.Range("F" & lRowInv).Interior.ColorIndex = 6
        invWS.Range("G" & lRowInv) = "MATCHED"
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
well , it works for two example and will not happen in third example should work every time when add new data after highlighted cell in ACCOUNT sheet , also work when change in INVOICE sheet.
 
Upvote 0
Try:
VBA Code:
Sub CompareValues()
    Application.ScreenUpdating = False
    Dim invWS As Worksheet, accWS As Worksheet, lRowInv As Long, lRowAcc As Long
    Set invWS = Sheets("INVOICE")
    Set accWS = Sheets("ACCOUNT")
    lRowInv = invWS.Range("F" & Rows.Count).End(xlUp).Row
    lRowAcc = accWS.Range("E" & Rows.Count).End(xlUp).Row
    If accWS.Range("E" & lRowAcc).Value > invWS.Range("F" & lRowInv).Value Or accWS.Range("E" & lRowAcc).Value < invWS.Range("F" & lRowInv).Value Then
        accWS.Range("E" & lRowAcc).Interior.ColorIndex = 3
        accWS.Range("F" & lRowAcc) = "PLUS " & accWS.Range("E" & lRowAcc).Value - invWS.Range("F" & lRowInv).Value
        invWS.Range("F" & lRowInv).Interior.ColorIndex = 3
        invWS.Range("G" & lRowInv).Value = "DEFICIT " & invWS.Range("F" & lRowInv).Value - accWS.Range("E" & lRowAcc).Value
    ElseIf accWS.Range("E" & lRowAcc).Value = invWS.Range("F" & lRowInv).Value Then
        accWS.Range("E" & lRowAcc).Interior.ColorIndex = 6
        accWS.Range("F" & lRowAcc) = "MATCHED "
        invWS.Range("F" & lRowInv).Interior.ColorIndex = 6
        invWS.Range("G" & lRowInv) = "MATCHED"
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
when show minus value show show DEFICIT word , not PLUS as the last cell in ACCOUNT sheet and alike should be PLUS value, not DEFICIT word in INVOICE sheet as last cell show.



COMPARE.xlsm
ABCDEF
1DATEINFODEBITCREDITBALANCE
201/11/2024SALES INVOICE NO BSJ20002,000.002,000.00
302/11/2024SALES INVOICE NO BSJ20013,000.005,000.00
403/11/2024SALES RETURNS INVOICE NO BSJ7893,000.002,000.00
503/11/2024VOUCHER NO VBGH67881,500.00500.00
604/11/2024SALES INVOICE NO BSJ200315,000.0015,500.00
704/11/2024SALES INVOICE NO BSJ200413,000.0028,500.00PLUS 15
805/11/2024SALES INVOICE NO BSJ200515,000.0043,500.00
905/11/2024SALES INVOICE NO BSJ20061,000.0044,500.00
1005/11/2024VOUCHER NO VBGH67891,000.0043,500.00
1105/11/2024VOUCHER NO VBGH67905,000.0038,500.00MATCHED
1205/11/2024SALES INVOICE NO BSJ20072,000.0040,500.00
1305/11/2024SALES INVOICE NO BSJ20083,000.0043,500.00
1405/11/2024SALES INVOICE NO BSJ20095,000.0048,500.00PLUS -1050
1506/11/2024SALES INVOICE NO BSJ20105,001.0053,501.00
1607/11/2024SALES INVOICE NO BSJ20115,002.0058,503.00
1708/11/2024SALES INVOICE NO BSJ20125,003.0063,506.00
1809/11/2024SALES INVOICE NO BSJ20135,004.0048,000.00PLUS -11550
ACCOUNT
Cell Formulas
RangeFormula
E2E2=C2-D2
E3:E17E3=E2+C3-D3



COMPARE.xlsm
BCDEFGH
21ITEMBRANDQTYPRICEBALANCE
221BJS 13R22.5 MA11 KOR10255525550
232KM 13R22.5 MA11 KOR12200024000
243KM 13R22.5 MA11 KOR10100010000
25TOTAL3259550DEFICIT 11550
INVOICE
Cell Formulas
RangeFormula
F22:F23F22=D22*E22
D25,F25D25=SUM(D22:D24)
 
Upvote 0
How about:
VBA Code:
Sub CompareValues()
    Application.ScreenUpdating = False
    Dim invWS As Worksheet, accWS As Worksheet, lRowInv As Long, lRowAcc As Long
    Set invWS = Sheets("INVOICE")
    Set accWS = Sheets("ACCOUNT")
    lRowInv = invWS.Range("F" & Rows.Count).End(xlUp).Row
    lRowAcc = accWS.Range("E" & Rows.Count).End(xlUp).Row
    If accWS.Range("E" & lRowAcc).Value > invWS.Range("F" & lRowInv).Value Or accWS.Range("E" & lRowAcc).Value < invWS.Range("F" & lRowInv).Value Then
        accWS.Range("E" & lRowAcc).Interior.ColorIndex = 3
        accWS.Range("F" & lRowAcc) = "DEFICIT " & accWS.Range("E" & lRowAcc).Value - invWS.Range("F" & lRowInv).Value
        invWS.Range("F" & lRowInv).Interior.ColorIndex = 3
        invWS.Range("G" & lRowInv).Value = "PLUS " & invWS.Range("F" & lRowInv).Value - accWS.Range("E" & lRowAcc).Value
    ElseIf accWS.Range("E" & lRowAcc).Value = invWS.Range("F" & lRowInv).Value Then
        accWS.Range("E" & lRowAcc).Interior.ColorIndex = 6
        accWS.Range("F" & lRowAcc) = "MATCHED "
        invWS.Range("F" & lRowInv).Interior.ColorIndex = 6
        invWS.Range("G" & lRowInv) = "MATCHED"
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
works but not always ,
It works for rows 13,17,19 but in row 20 return the same problem and alike in invoice sheet !
COMPARE.xlsm
ABCDEF
1DATEINFODEBITCREDITBALANCE
201/11/2024SALES INVOICE NO BSJ20002,000.002,000.00
302/11/2024SALES INVOICE NO BSJ20013,000.005,000.00
403/11/2024SALES RETURNS INVOICE NO BSJ7893,000.002,000.00
503/11/2024VOUCHER NO VBGH67881,500.00500.00
604/11/2024SALES INVOICE NO BSJ200315,000.0015,500.00
704/11/2024SALES INVOICE NO BSJ200413,000.0028,500.00DEFICIT -31050
805/11/2024SALES INVOICE NO BSJ200513,001.0041,501.00
906/11/2024SALES INVOICE NO BSJ200613,002.0054,503.00
1007/11/2024SALES INVOICE NO BSJ200713,003.0067,506.00
1108/11/2024SALES INVOICE NO BSJ200813,004.0080,510.00
1209/11/2024SALES INVOICE NO BSJ200913,005.0093,515.00
1310/11/2024SALES INVOICE NO BSJ201013,006.00106,521.00DEFICIT -958690
1411/11/2024SALES INVOICE NO BSJ201113,007.00119,528.00
1512/11/2024SALES INVOICE NO BSJ201213,008.00132,536.00
1613/11/2024SALES INVOICE NO BSJ201313,009.00145,545.00
1714/11/2024VOUCHER NO VBGH678975,995.0069,550.00MATCHED
1815/11/2024VOUCHER NO VBGH679010,000.0079,550.00
1916/11/2024VOUCHER NO VBGH679175,997.003,553.00DEFICIT -65997
2017/11/2024SALES INVOICE NO BSJ2014100,000.00103,553.00DEFICIT 34003
ACCOUNT
Cell Formulas
RangeFormula
E2E2=C2-D2
E3:E20E3=E2+C3-D3
 
Upvote 0
Please post the INVOICE sheet associated with the AMOUNT sheet in Post #9.
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,120
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