Checking Inequality in-between 2 Cells

kendiryakuphan

New Member
Joined
Jul 13, 2022
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
Hi there,

For my accounting homework, I need to construct a balance sheet in excel. As you may know, Total Assets should be equal to the sum of Total Liabilities and Equities. At the bottom, I have 2 cells, one of them represents the sum of asset items, whereas the other represents the sum of all liability and equity items.

I would like generate a macro, that verifies that 2 cells are equal to each other in terms of number. And if those 2 cells are not equal, it should not enable the user to close the excel or save the excel.

I am completely new to macros and have no idea how to satisfy that condition.

Could you please help me to write the code?

Thank you all in advance for your support.

Best,
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi there and welcome...

Not sure where your values are located but try the below:

1. Use the keyboard shortcut – ALT + F11 (hold the ALT key and press the F11 key). As soon as you do this, it will open a separate window for the Visual Basic editor.
2. Select ThisWorkbook and paste the code below into the window.

VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If Range("G2").Value = Range("H2").Value Then
        MsgBox "Values Match"
    Else
        Cancel = True
        MsgBox "Values do not Match. Please Check..."
    End If
End Sub

All you will need to do is change the Range Values to wherever your values are located. You can also customize the message in between the quotation marks.
 
Upvote 0
Hi there and welcome...

Not sure where your values are located but try the below:

1. Use the keyboard shortcut – ALT + F11 (hold the ALT key and press the F11 key). As soon as you do this, it will open a separate window for the Visual Basic editor.
2. Select ThisWorkbook and paste the code below into the window.

VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If Range("G2").Value = Range("H2").Value Then
        MsgBox "Values Match"
    Else
        Cancel = True
        MsgBox "Values do not Match. Please Check..."
    End If
End Sub

All you will need to do is change the Range Values to wherever your values are located. You can also customize the message in between the quotation marks.
Thank you a lot for your very quick response
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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