Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VBA MsgBox if two cells are not equal to one another

Posted on 2013-11-05
6
Medium Priority
?
1,037 Views
Last Modified: 2013-11-06
is it possible to put out an error message [box] if two cell do not equal one another.

I would like a message box to look up the word Balance and then if the cell 3 to it's right does not equal the cell 5 to the right of balance issue an error message saying Does not balance. Both cells should contain 0 and the result should be 0

Here's hoping!

Thanks
0
Comment
Question by:Jagwarman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 10

Expert Comment

by:Anthony Berenguel
ID: 39624362
Should it look for 'balance' in the entire sheet or a specific column? Can you provide an example workbook?
0
 
LVL 48

Expert Comment

by:Dale Fye
ID: 39624374
I would recommend using conditional formatting.
0
 

Author Comment

by:Jagwarman
ID: 39624457
I prefer to use vba because this is part of a much bigger VBA.

file attached. The Bals at the foot in row 50 should both be 0 which would mean the inputter has input a wrong amount somewhere.

If they insert new rows row 50 will move.

Thanks
Data-the-same-or-not.xlsx
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 81

Expert Comment

by:byundt
ID: 39624739
Here is a macro that will search column K for the word "Balance". It will then compare the cells 3 and 5 columns to the right. If they differ by more than 0.005, then a message is displayed.
Sub TestBalance()
Dim cel As Range
With ActiveSheet
    On Error Resume Next
    Set cel = .Columns("K").Find("Balance", LookAt:=xlPart)
    If Not cel Is Nothing Then
        If Abs(cel.Offset(0, 3).Value - cel.Offset(0, 5).Value) >= 0.005 Then _
            MsgBox "Does not balance"
    End If
    On Error GoTo 0
End With
End Sub

Open in new window

0
 
LVL 81

Accepted Solution

by:
byundt earned 2000 total points
ID: 39624765
I added a test for whether the balance is zero or not.
Sub TestBalance()
Dim cel As Range
With ActiveSheet
    On Error Resume Next
    Set cel = .Columns("K").Find("Balance", LookAt:=xlPart)
    If Not cel Is Nothing Then
        If Abs(cel.Offset(0, 3).Value - cel.Offset(0, 5).Value) >= 0.005 Then
            MsgBox "Does not balance"
        ElseIf Abs(cel.Offset(0, 3).Value) >= 0.005 Then
            MsgBox "Balance should be zero"
        End If
    End If
    On Error GoTo 0
End With
End Sub

Open in new window

For both tests, I am looking for discrepancies larger than 0.005 to eliminate nuisance warning messages caused by truncation after 15 decimal places (the limit of Excel's accuracy).
0
 

Author Comment

by:Jagwarman
ID: 39627440
byundt this is exactly what I am looking for. Many thanks. Brilliant
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

596 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question