+ Reply to Thread
Results 1 to 15 of 15

Data validation - Whole Numbers

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-16-2006
    Posts
    349

    Data validation - Whole Numbers

    how can I set a cell so that you can only type in 1 , 2 , 3, 4, 5

    I know I can use the validation but people can just copy a cell and paste it over.

  2. #2
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Under allow > select whole numbers

    Min 1
    Max 5

    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  3. #3
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127
    Either you can set up a validation (as you mentioned) AND protect your worksheet to prevent people from copying/pasting over the validation, or you can use VBA (if you don't mind that) to prevent people from entering an incorrect value.

  4. #4
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Sorry

    Misread the question.

    See if link helps

    http://www.j-walk.com/ss/excel/tips/tip98.htm

    VBA Noob

  5. #5
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Or a event macro. Right click sheet > select view code and paste in the below. Change range as required

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("A1:A5")) Is Nothing Then
    If Target.Value > 5 Then
    Application.Undo
    MsgBox "Please enter number between 1 & 5"
    End If
    End If
    End Sub
    VBA Noob

  6. #6
    Forum Contributor
    Join Date
    12-16-2006
    Posts
    349

    Allow ?

    Sorry where can I specify allow ?

  7. #7
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988

  8. #8
    Forum Contributor
    Join Date
    12-16-2006
    Posts
    349

    Validation

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("A1:A5")) Is Nothing Then
    If Target.Value > 5 Then
    Application.Undo
    MsgBox "Please enter number between 1 & 5"
    End If
    End If
    End Sub
    HOW CAN I ADPAT THIS SO THAT Z3:Z500 IS RESTRICTED TO 1 , 2 OR 3 AS WELL AS THE ABOVE.

  9. #9
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Change to

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("Z3:Z500")) Is Nothing Then
    If Target.Value > 3 Then
    Application.Undo
    MsgBox "Please enter number between 1 & 3"
    End If
    End If
    End Sub
    VBA Noob

  10. #10
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127
    This is just a minor change to VBA Noob's code to disallow decimal numbers from being used in place of whole numbers:

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("Z3:Z500")) Is Nothing Then
    If Target.Value > 3 Or Target.Value <> Int(Target.Value) Then
    Application.Undo
    MsgBox "Please enter number between 1 & 3"
    End If
    End If
    End Sub

  11. #11
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Try

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("A1:A5")) Is Nothing Then
    If Target.Value > 5 Then
    Application.Undo
    MsgBox "Please enter number between 1 & 5"
    End If
    End If
    If Not Intersect(Target, Range("Z3:Z500")) Is Nothing Then
    If Target.Value > 3 Then
    Application.Undo
    MsgBox "Please enter number between 1 & 3"
    End If
    End If
    End Sub
    VBA Noob

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1