Compare two sheets based on lastrow with highlighted cells

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
613
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
 
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

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
ok it just works if the value in ACCOUNT sheet bigger than value in INVOICE sheet ,but if the value in ACCOUNT sheet smaller than value in INVOICE sheet then will show the same problem !
 
Upvote 0
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 = 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
Great !(y)
last thing : can you show the number as formatting like this #,##0.00 after DEFICIT or PLUS words,please?
 
Upvote 0
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 " & Format(accWS.Range("E" & lRowAcc).Value - invWS.Range("F" & lRowInv).Value, "#,##0.00")
        invWS.Range("F" & lRowInv).Interior.ColorIndex = 3
        invWS.Range("G" & lRowInv).Value = "DEFICIT " & Format(invWS.Range("F" & lRowInv).Value - accWS.Range("E" & lRowAcc).Value, "#,##0.00")
    ElseIf accWS.Range("E" & lRowAcc).Value < invWS.Range("F" & lRowInv).Value Then
        accWS.Range("E" & lRowAcc).Interior.ColorIndex = 3
        accWS.Range("F" & lRowAcc) = "DEFICIT " & Format(accWS.Range("E" & lRowAcc).Value - invWS.Range("F" & lRowInv).Value, "#,##0.00")
        invWS.Range("F" & lRowInv).Interior.ColorIndex = 3
        invWS.Range("G" & lRowInv).Value = "PLUS " & Format(invWS.Range("F" & lRowInv).Value - accWS.Range("E" & lRowAcc).Value, "#,##0.00")
    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
Solution

Forum statistics

Threads
1,223,952
Messages
6,175,595
Members
452,657
Latest member
giadungthienduyen

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