Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
AM AM is offline
external usenet poster
 
Posts: 41
Default Compare cell and msg

Hi All,

I need some help woth code.
On cell B2 when focus is lost then compare its value with the value of A2,
if b2 value is less then A2 then I want to popup a message saying value in B2
cannot be less then A2 and bring the focus back on B2

Thank In Advance
AM
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 421
Default Compare cell and msg

Hi AM.

You can achieve the stated objective
without code; try using Excel's Data
Validation tool:


Menu | Data | Validation



---
Regards.
Norman
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Compare cell and msg

However, if your project needs to use code. Try the macro below. If needs
to be pasted into the worksheet code window. Right click the sheet tab and
select View Code from the drop down menu. Copy this code and paste it into
the code window.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target = Range("B2") Then
If Range("B2") < Range("A2") Then
MsgBox "CELL B2 CANNOT BE LESS THAN CELL A2, TRY AGAIN"
Range("B2").Select
End If
End If
End Sub




"AM" wrote:

Hi All,

I need some help woth code.
On cell B2 when focus is lost then compare its value with the value of A2,
if b2 value is less then A2 then I want to popup a message saying value in B2
cannot be less then A2 and bring the focus back on B2

Thank In Advance
AM

  #4   Report Post  
Posted to microsoft.public.excel.programming
AM AM is offline
external usenet poster
 
Posts: 41
Default Compare cell and msg

Thanks, This is exactly what I needed and it works. Appreciate everyones help.

AM

"JLGWhiz" wrote:

However, if your project needs to use code. Try the macro below. If needs
to be pasted into the worksheet code window. Right click the sheet tab and
select View Code from the drop down menu. Copy this code and paste it into
the code window.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target = Range("B2") Then
If Range("B2") < Range("A2") Then
MsgBox "CELL B2 CANNOT BE LESS THAN CELL A2, TRY AGAIN"
Range("B2").Select
End If
End If
End Sub




"AM" wrote:

Hi All,

I need some help woth code.
On cell B2 when focus is lost then compare its value with the value of A2,
if b2 value is less then A2 then I want to popup a message saying value in B2
cannot be less then A2 and bring the focus back on B2

Thank In Advance
AM

  #5   Report Post  
Posted to microsoft.public.excel.programming
AM AM is offline
external usenet poster
 
Posts: 41
Default Compare cell and msg

Norman,

Thanks for your response, I am not sure how I can compare value in 2 cell
and give message using validation? Can you please give little mroe details?

Thanks
AM

"Norman Jones" wrote:

Hi AM.

You can achieve the stated objective
without code; try using Excel's Data
Validation tool:


Menu | Data | Validation



---
Regards.
Norman



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 421
Default Compare cell and msg

Hi JLGWhiz,

To avoid a Run-time 13 error if the value
of a multi-cell range is changed, more
robust might be the following version:

'=========
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Rng As Range
Dim Rng2 As Range

With Me
Set Rng = .Range("A2")
Set Rng2 = .Range("B2")
End With

If Not Intersect(Rng2, Target) Is Nothing Then
If Rng2.Value < Rng.Value Then
MsgBox "CELL B2 CANNOT BE LESS " _
& "THAN CELL A2, TRY AGAIN"
Rng2.Select
End If
End If
End Sub
'<<=========



---
Regards.
Norman

"JLGWhiz" wrote in message
...
However, if your project needs to use code. Try the macro below. If
needs
to be pasted into the worksheet code window. Right click the sheet tab
and
select View Code from the drop down menu. Copy this code and paste it
into
the code window.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target = Range("B2") Then
If Range("B2") < Range("A2") Then
MsgBox "CELL B2 CANNOT BE LESS THAN CELL A2, TRY AGAIN"
Range("B2").Select
End If
End If
End Sub




"AM" wrote:

Hi All,

I need some help woth code.
On cell B2 when focus is lost then compare its value with the value of
A2,
if b2 value is less then A2 then I want to popup a message saying value
in B2
cannot be less then A2 and bring the focus back on B2

Thank In Advance
AM


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 421
Default Compare cell and msg

And, to be consistent with my use of
an object variable for B2, it would be
better to replace:

MsgBox "CELL B2 CANNOT BE LESS " _
& "THAN CELL A2, TRY AGAIN"


with
MsgBox Prompt:="CELL " & Rng2.Address(0, 0) _
& " CANNOT BE LESS " _
& "THAN CELL A2, TRY AGAIN"

---
Regards.
Norman


"Norman Jones" wrote in message
...
Hi JLGWhiz,

To avoid a Run-time 13 error if the value
of a multi-cell range is changed, more
robust might be the following version:

'=========
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Rng As Range
Dim Rng2 As Range

With Me
Set Rng = .Range("A2")
Set Rng2 = .Range("B2")
End With

If Not Intersect(Rng2, Target) Is Nothing Then
If Rng2.Value < Rng.Value Then
MsgBox "CELL B2 CANNOT BE LESS " _
& "THAN CELL A2, TRY AGAIN"
Rng2.Select
End If
End If
End Sub
'<<=========



---
Regards.
Norman


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compare text string of a cell in Column A VS another cell in Colum Tan New Users to Excel 2 August 1st 07 09:45 AM
Compare text string of a cell in Column A VS another cell in Colum Tan Excel Discussion (Misc queries) 1 August 1st 07 09:03 AM
Compare text string of a cell in Column A VS another cell in Colum Tan Excel Worksheet Functions 1 August 1st 07 09:01 AM
Compare text string of a cell in Column A VS another cell in Colum Tan Excel Programming 0 July 30th 07 05:12 PM
How do I compare cells and if FALSE compare to next cell in EXCEL Cindie Excel Worksheet Functions 0 March 24th 06 05:29 PM


All times are GMT +1. The time now is 10:30 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"