Private sub question

Vtookup

Board Regular
Joined
May 30, 2017
Messages
121
Office Version
  1. 2016
  2. 2013
Hi.
I want to check cell if A2>A1.
If its true then A2 will turn blank for user to re-enter. And if it's false, then do nothing.
This Workbook will only have one sheet.
I have used If, Then, End if, Else. but without success. I fall short on Do Nothing code.
Question, What private sub ??? (Declaration?) and need help how to write the code?
Thanks in advance.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try this:
VBA Code:
Sub My_Sub()
If Range("A2").Value > Range("A1").Value Then Range("A2").Value = ""
End Sub
 
Upvote 0
I think I'm interpreting the requirement differently - the following code should be put in the sheet code module of the sheet in question. It will trigger automatically when A2's value changes via input:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("A2")) Is Nothing Then
        If Not IsNumeric(Target.Value) Then
            MsgBox "Enter numbers only in cell A2"
            Target = vbNullString
            Target.Select
            Exit Sub
        End If
        
        If Target > Target.Offset(-1) Then
            MsgBox "Enter a number smaller than A1's value"
            Target = vbNullString
            Target.Select
        End If
    End If
End Sub
 
Upvote 1
This code is worksheet module (not general module). It will be triggered any change in specific range in sheet.

Right click on tab's name, View Code, then paste it into

VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Range("A2:B100") ' adjust to the actual input range
If Intersect(Target, rng) Is Nothing Then Exit Sub
If IsNumeric(Target) And Target <= Target.Offset(-1) Then Exit Sub
Target = ""
End Sub
 
Upvote 1
If this was to be a Worksheet change again script I would do it this way.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$2" Then
If Target.Value > Target.Offset(-1, 0).Value Then
Target.Value = ""
Target.Select
End If

End If
End Sub
 
Upvote 1
Hi All,
My Aswer Is This, kevin9999 and bebo021999
Thanks for the help. All codes works.
It made my day. Nice touch with Target.Select
Thank You.
 
Upvote 0
Hi All,
My Aswer Is This, kevin9999 and bebo021999
Thanks for the help. All codes works.
It made my day. Nice touch with Target.Select
Thank You.
Glad we could help, and thanks for the feedback 👍
 
Upvote 0

Forum statistics

Threads
1,215,149
Messages
6,123,311
Members
449,095
Latest member
Chestertim

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