values in column C must be larger than in B

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
334
Office Version
  1. 2016
Platform
  1. Windows
I have a sheet for data entry in cols B and C. Values in col C must be larger than in B, I have the below code to check this.

But when I return to B, and I make a change there, I can still enter an invalid number. I could trap this through similar code for col B, but then I drive in circles...

Any suggestions ?

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

Set col_B_rng = Range("B2:B10")
Set col_C_rng = Range("C2:C10")

'check that value in col C is larger than in col B

If Not Application.Intersect(Target, col_B_rng) Is Nothing Then
    Target.Offset(0, 1).Select
End If

If Target.Count = 1 And Not Application.Intersect(Target, col_C_rng) Is Nothing Then
        If Target.Value <= Target.Offset(0, -1).Value Then
            Application.EnableEvents = False
            Target.Select
            MsgBox ("values in col C must be larger than in col B"), vbCritical
            Application.Undo
            Application.EnableEvents = True
        Else
            Target.Offset(1, -1).Select
        End If
End If

End Sub
 

Attachments

  • larger.png
    larger.png
    18.7 KB · Views: 4

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Please try the following on a copy of your workbook:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.CountLarge = 1 And Not Intersect(Range("B2:C10"), Target) Is Nothing Then
        Application.EnableEvents = False
        If Cells(Target.Row, 2) >= Cells(Target.Row, 3) Then
            MsgBox "values in col C must be larger than in col B"
            Application.Undo
            Target.Select
        End If
        Application.EnableEvents = True
    End If
End Sub
 
Upvote 0
Solution
Thanks Kevin; this works fine.

However, there still is an issue: when I put e.g. the value 4 in B2, and 5 in C2, then OK. But say I need 6 in B2 and 7 in C2. Then when I go back to B2 I have a problem.
 
Upvote 0
Yes, you would need to put the value in column C first in such cases - unfortunately, I can't think of a workaround to that.
 
Upvote 0
Thanks Kevin. I can't think of a gentle workaround either, however I've solved it in this way:


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

If Target.Cells.CountLarge = 1 And Not Intersect(Range("C2:C10"), Target) Is Nothing Then
    Application.EnableEvents = False
    If Cells(Target.Row, 2) >= Cells(Target.Row, 3) Then
        MsgBox "values in col C must be larger than in col B"
        Application.Undo
        Target.Select
    Else
        Target.Offset(1, -1).Select
    End If
    Application.EnableEvents = True
End If
    
If Target.Cells.CountLarge = 1 And Not Intersect(Range("B2:B10"), Target) Is Nothing Then
    Application.EnableEvents = False
    If Cells(Target.Row, 2) >= Cells(Target.Row, 3) Then
        Cells(Target.Row, 3) = Cells(Target.Row, 2)
    End If
    Target.Offset(0, 1).Select
    Application.EnableEvents = True
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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