简体   繁体   English

将单元格限制为输入特定数字范围的特定顺序

[英]Restricting cells to a specific order of entering a specific range of numbers

Here's the scenario. 这是场景。 I have in columns E6:E309 all the job numbers. 我在E6:E309栏中有所有职位编号。 I would like the sales person to come and type in the relative cell in D6:D309 which job is to be drawn first by typing in the range D6:D309 any number from 1 to 303. If a sales person entered in column D11 "1", that means that job number is E11 is to be cut first. 我希望销售人员来输入D6:D309中的相对单元格,首先通过在范围D6:D309中键入1到303之间的任何数字来绘制该作业。如果销售人员在D11列中输入“ 1 ”,这意味着作业编号为E11将首先被剪切。 The hard part is the following two conditions: 困难的部分是以下两个条件:

1st Condition: The first entry in any cell belonging to the range D6:309 should be number 1, the second is number 2, the third is number 3, and so on up till number 309. So, the sales person can not enter the number "2" in any cell belonging to D6:D309, unless the number "1" is entered somewhere else in any cell belonging to the range D6:D309 is entered, and so on. 第一个条件:属于范围D6:309的任何单元中的第一个条目应为数字1,第二个为数字2,第三个为数字3,依此类推,直到309。 因此,销售人员无法输入除非在属于范围D6:D309的任何单元格中的其他位置输入数字“ 1”,否则在属于D6:D309的任何单元格中的数字“ 2”,依此类推。

2nd Condition: There's no specific order for which cells are entered before the other. 第二个条件:输入单元格之前没有特定的顺序。 The salesperson can start entering in any cell, per se D10 by entering "1", and then move to D18 where he can only enter "2", and then back to D3 where he can only enter "3", and so on. 销售人员可以通过输入“ 1”开始在任何单元格中输入D10本身,然后移动到D18,他只能输入“ 2”,然后返回D3,他只能输入“ 3”,依此类推。

Your help will be very much appreciated. 非常感谢您的帮助。

This can be done with some clever custom validations. 这可以通过一些聪明的自定义验证来完成。 To place a customer validation, select the cell(s) you want to apply this validation and then select Data->Data Validation. 要进行客户验证,请选择要应用此验证的单元格,然后选择“数据”->“数据验证”。 In the form under settings, select Custom in the "Allow" dropdown and enter the formula. 在设置下的表单中,从“允许”下拉列表中选择“自定义”,然后输入公式。

In detail, you'll need to following three validations: 详细来说,您需要进行以下三个验证:

  1. Cell D6: =D6=(MAX($D$7:$D$309,0)+1) 单元格D6: =D6=(MAX($D$7:$D$309,0)+1)
  2. Cells D7:D308 (make sure cell D7 is the active cell in the selection: =D7=(MAX($D$6:$D6,$D8:$D$309,0)+1) 单元格D7:D308(确保单元格D7是选择中的活动单元格: =D7=(MAX($D$6:$D6,$D8:$D$309,0)+1)
  3. Cell D309: =D309=(MAX($D$6:$D$308,0)+1) 单元格D309: =D309=(MAX($D$6:$D$308,0)+1)

If you want you can add a custom error message in the "Error Alert" tab to explain the logic to the user. 如果需要,可以在“错误警报”选项卡中添加自定义错误消息,以向用户说明逻辑。

Be aware: While this solution fulfills your conditions, you'll run into problems the moment any entry is deleted! 请注意:尽管此解决方案可以满足您的条件,但是删除任何条目时,您都会遇到问题! In this the validations still force the user to enter the next highest number, so the deleted number cannot be entered unless all other numbers above it have be deleted! 在这种情况下,验证仍然会强制用户输入下一个最高编号,因此,除非已删除其上方的所有其他编号,否则无法输入已删除的编号!

Following Worksheet_Change event should work. 以下Worksheet_Change事件应该起作用。 It does the following: 它执行以下操作:

1) check whether entered value is a number if not display message and exit 1)检查所输入的值是否是一个数字,如果不显示消息并退出
2) check for correct number to be entered 2)检查要输入的正确号码

Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("D6:D309")) Is Nothing Or Target.Cells.Count > 1 Then Exit Sub 'check for range

    Application.EnableEvents = False
    Dim countPrevNum As Long, largeNum As Long, currNum As Long

    If Not IsNumeric(Target.Value) Then     'check if entered value is number
        MsgBox "Enter a number"
        Target.Cells.Clear
        Target.Cells.Select
        Application.EnableEvents = True
        Exit Sub
    End If

    currNum = Target.Value      'value entered
    Target.Cells.Clear          'clear the target cell

    If WorksheetFunction.CountA(Range("D6:D309")) Then
        largeNum = WorksheetFunction.Large(Range("D6:D309"), 1)   'get the largest number in the range
    End If

    If largeNum <> currNum - 1 Then     'check if previous largest number is equal to currNum-1
        MsgBox "Number to be entered : " & largeNum + 1
        Target.Cells.Select         'select the target cell
    Else
        Target.Value = currNum      'enter the value target cell
    End If
    Application.EnableEvents = True
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM