Allow user to input specific text and number-need VBA code

Thilan

New Member
Joined
Jul 13, 2021
Messages
10
Office Version
  1. 2016
Platform
  1. Windows
hi anyone can help me on below,

I want users to enter specific text (AB, DO, SL) and numbers (-10 to 20) in the cell range(A1:B10, A20: B30). Users should be able to enter only allowed data.
if not cell should be clear.
I don't want to use data validation impact I need VBA codes.

please help me.
Thanks.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Welcome to the MrExcel board!

numbers (-10 to 20)
It is unclear whether they must be whole numbers or not. Code below allows values like -8.62 or 19.99.

See if this Worksheet_Change event code does what you want. To implement ..
1. Right click the sheet name tab and choose "View Code".
2. Copy and Paste the code below into the main right hand pane that opens at step 1.
3. Close the Visual Basic window & test.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Changed As Range, c As Range
  Dim sCleared As String
 
  Set Changed = Intersect(Target, Range("A1:B10,A20:B30"))
  If Not Changed Is Nothing Then
    Application.EnableEvents = False
    For Each c In Changed
      Select Case c.Value
        Case "AB", "DO", "SL", -10 To 20
        Case Else
          sCleared = sCleared & vbLf & c.Address(0, 0) & " (" & c.Value & ")"
          c.ClearContents
      End Select
    Next c
    Application.EnableEvents = True
    If Len(sCleared) > 0 Then MsgBox "Invalid entry removed from" & sCleared
  End If
End Sub
 
Upvote 0
Solution
Welcome to the MrExcel board!


It is unclear whether they must be whole numbers or not. Code below allows values like -8.62 or 19.99.

See if this Worksheet_Change event code does what you want. To implement ..
1. Right click the sheet name tab and choose "View Code".
2. Copy and Paste the code below into the main right hand pane that opens at step 1.
3. Close the Visual Basic window & test.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Changed As Range, c As Range
  Dim sCleared As String
 
  Set Changed = Intersect(Target, Range("A1:B10,A20:B30"))
  If Not Changed Is Nothing Then
    Application.EnableEvents = False
    For Each c In Changed
      Select Case c.Value
        Case "AB", "DO", "SL", -10 To 20
        Case Else
          sCleared = sCleared & vbLf & c.Address(0, 0) & " (" & c.Value & ")"
          c.ClearContents
      End Select
    Next c
    Application.EnableEvents = True
    If Len(sCleared) > 0 Then MsgBox "Invalid entry removed from" & sCleared
  End If
End Sub
yes its working grate. well appreciated your effort. thank you very much.
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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