Pop Up A Message Box If Cells Value In A Column Is Greater Or Less Than Cells Value In Another Column

mergim

New Member
Joined
Nov 24, 2020
Messages
49
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have some issues doing some VBA Coding. What I am trying to do is, if values in a column is greater than the corresponding values in another column (e.g. A1 and B1), it should show a messagebox. In my scenario it is regarding a date. So fx. if the date values in column a is greater/after/far ahead than values in column b, it should show error, because fx an award date should always be before production starts, as you dont start production before you get awarded. I hope you guys can help! Thank you in advance :;D
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hello,

Just to note, if the cell has an error inside it will cause an error in VBA
For the VBA code, is it ran in a macro or when a cell value is updated?
 
Upvote 0
It is when a cell value is changed or updated. People tend to change award date but forget to change production start - so I want it to be a kind of a reminder :D
 
Upvote 0
If you add this into the sheet you are working on, I think this might be what you're looking for
Be aware, if there are multiple cells that are greater than the corresponding column then it will do a message box for each cell
I would recommend changing them all first before adding this code

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range

For Each Cell In Range("A:A")
If Not Cell.Value = "" Then
If Cell.Value > Cell.Offset(0, 1).Value Then
    Cell.Select
    MsgBox "Value in Cell " & Cell.Address & " is greater than corresponding column"
End If
End If
Next Cell

End Sub
 
Upvote 0
In the cell "C1" create virtual calculation. For exaple insert "=(A1*1)"
In the formulas tab turn on calculation option to the " Automatic"
Open code editor and put this code in the corresponding sheet.
VBA Code:
Option Explicit

Private Sub Worksheet_Calculate()
    
     If Range("A1").Value > Range("B1").Value Then MsgBox ("ERROR")
     
End Sub
 
Upvote 0
That didn't work... What I am trying to do is - If values in column D is greater than values in Column G, then a msgbox should appear. Right now there are no greater value in column D than G. Note that it is the rows that should be compared, so D1 with G1, D2 with G2 and so on... So something like "D:D" and "G:G".
Thank you :D
 
Upvote 0
That didn't work... What I am trying to do is - If values in column D is greater than values in Column G, then a msgbox should appear. Right now there are no greater value in column D than G. Note that it is the rows that should be compared, so D1 with G1, D2 with G2 and so on... So something like "D:D" and "G:G".
Thank you :D
Try this in your worksheet...
VBA Code:
Option Explicit

Private Sub Worksheet_Calculate()
     'Remarks Column D=4 G=7
     If ActiveCell.Column = 4 Then
        If Range("D" & ActiveCell.Row).Value > Range("G" & ActiveCell.Row).Value Then
            MsgBox "Your Message Here..."
        End If
     End If
End Sub
 
Last edited:
Upvote 0
That didn't work... What I am trying to do is - If values in column D is greater than values in Column G, then a msgbox should appear. Right now there are no greater value in column D than G. Note that it is the rows that should be compared, so D1 with G1, D2 with G2 and so on... So something like "D:D" and "G:G".
Thank you :D
In your original post you said:
another column (e.g. A1 and B1)
 
Upvote 0
If you want to do that for all ranges with data in the column "D",
you can do something like this.
VBA Code:
Sub CompareRanges()

    Dim varNRows As Long
    Dim varWorksheetName As Worksheet
    Dim varRange1, varRange2 As Range
    
    Set varWorksheetName = Sheets("YourSheetName")
    varNRows = varWorksheetName.Cells(Rows.Count, "D").End(xlUp).Row
    Set varRange2 = varWorksheetName.Range("D1:D" & varNRows)
    
    For Each varRange1 In varRange2
       If varRange1.Value > varRange1.Offset(0, 3).Value Then
          MsgBox ("ERROR! cell 'D" & varRange1.Row & "'")
          Exit Sub
       End If
    Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,467
Members
448,965
Latest member
grijken

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