Excel 如何控制哪些单元格可以接收用户输入以及光标移动到每个单元格的顺序?

Excel 如何控制哪些单元格可以接收用户输入以及光标移动到每个单元格的顺序?,excel,vba,Excel,Vba,我希望只允许用户按以下顺序在下面列出的单元格中输入值: D3、C3、B9、B3、E2、D4、G4、I4、D5、G5、I5、D6、G6、I6、D7、G7、I7、D8、G8和I8 打开excel并选择您提到的单元格。您可以通过在选择单元格时按住Ctrl键来完成此操作。现在右键单击并选择“设置单元格格式”,然后转到“保护”选项卡。在“保护”选项卡中,取消选中“锁定”复选框。现在保护工作表并确保选中“选择未锁定的单元格”选项(列表中的第二个复选框) 下面是使用vba控制订单的代码 右键单击sheet1选

我希望只允许用户按以下顺序在下面列出的单元格中输入值:

D3、C3、B9、B3、E2、D4、G4、I4、D5、G5、I5、D6、G6、I6、D7、G7、I7、D8、G8和I8


打开excel并选择您提到的单元格。您可以通过在选择单元格时按住Ctrl键来完成此操作。现在右键单击并选择“设置单元格格式”,然后转到“保护”选项卡。在“保护”选项卡中,取消选中“锁定”复选框。现在保护工作表并确保选中“选择未锁定的单元格”选项(列表中的第二个复选框)

下面是使用vba控制订单的代码

  • 右键单击sheet1选项卡和“查看代码”

  • 将以下代码粘贴到该图纸模块中

    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    On Error GoTo enditall
    Application.EnableEvents = False
    i = Array("D3", "C3", "B9", "B3", "E2", "D4", "G4", "I4", "D5", "G5", "I5", "D6", "G6", "I6", "D7", "G7", "I7", "D8", "G8", "I8")
    k = Replace(Target.Cells.Address, "$", "")
    If k = i(j) Then
    Sheet1.Unprotect
    Sheet1.Range(i(j)).Locked = True
    Sheet1.Range(i(j + 1)).Locked = False
    Sheet1.Range(i(j + 1)).Select
    j = j + 1
    Sheet1.Protect
    End If
    enditall:
    Sheet1.Protect
    Application.EnableEvents = True
    End Sub
    
  • 现在,在同一窗口中的sheet1上单击鼠标右键,然后选择“插入”。从插入菜单中单击模块。现在选择创建模块并粘贴以下代码

    Public j
    
    Sub Settings()
    j = 0
    Sheet1.Unprotect
    Sheet1.Cells.Locked = True
    Sheet1.Range("D3").Locked = False
    Sheet1.Range("D3").Select
    Sheet1.Protect
    End Sub
    
  • 现在,每次要向工作表输入数据时,都要运行宏设置

  • 下面是代码的注释版本

    'The code below should come in a module and should be run every time a change is required in the sheet
    Public j 'declaring j as a public variable so that it can be accessed from any procedure in the excel project
    
    Sub Settings()
    j = 0 'sets j as zero
    Sheet1.Unprotect 'unprotect the sheet
    Sheet1.Cells.Locked = True 'locks all the cells in the sheet
    Sheet1.Range("D3").Locked = False 'unlocks the first cell D3 and makes it editable
    Sheet1.Range("D3").Select 'selects the cell D3
    Sheet1.Protect 'reprotects the sheet
    End Sub
    
    
    
    'Following code should be entered in the code of sheet where the values need to be entered
    Private Sub Worksheet_Change(ByVal Target As Excel.Range) 'the code runs when a cell in the excel sheet is changed
    On Error GoTo enditall 'this handles error
    Application.EnableEvents = False 'excel events are disabled
    i = Array("D3", "C3", "B9", "B3", "E2", "D4", "G4", "I4", "D5", "G5", "I5", "D6", "G6", "I6", "D7", "G7", "I7", "D8", "G8", "I8") 'array with cells in order
    k = Replace(Target.Cells.Address, "$", "") 'finds the cell address where the change was made
    If k = i(j) Then 'checks whether the change was made in a cell in the array, we set the value of j to zero by running the macro settings shown below
    Sheet1.Unprotect 'unprotects the sheet to make the changes
    Sheet1.Range(i(j)).Locked = True 'makes the correspondig cell in the array locked after editing
    Sheet1.Range(i(j + 1)).Locked = False 'unlocks the next cell in the array
    Sheet1.Range(i(j + 1)).Select 'selects the next cell in the array
    j = j + 1 'increments the value of j by 1
    Sheet1.Protect 'reprotects the sheet
    End If
    enditall: 'the code below will run on an error, this code will run when value of j becomes more than the number of elements in array k
    Sheet1.Protect 'protect the sheet
    Application.EnableEvents = True 'enables excel events
    End Sub
    

    请参见位于

    的示例文件打开excel并选择您提到的单元格。您可以通过在选择单元格时按住Ctrl键来完成此操作。现在右键单击并选择“设置单元格格式”,然后转到“保护”选项卡。在“保护”选项卡中,取消选中“锁定”复选框。现在保护工作表并确保选中“选择未锁定的单元格”选项(列表中的第二个复选框)

    下面是使用vba控制订单的代码

  • 右键单击sheet1选项卡和“查看代码”

  • 将以下代码粘贴到该图纸模块中

    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    On Error GoTo enditall
    Application.EnableEvents = False
    i = Array("D3", "C3", "B9", "B3", "E2", "D4", "G4", "I4", "D5", "G5", "I5", "D6", "G6", "I6", "D7", "G7", "I7", "D8", "G8", "I8")
    k = Replace(Target.Cells.Address, "$", "")
    If k = i(j) Then
    Sheet1.Unprotect
    Sheet1.Range(i(j)).Locked = True
    Sheet1.Range(i(j + 1)).Locked = False
    Sheet1.Range(i(j + 1)).Select
    j = j + 1
    Sheet1.Protect
    End If
    enditall:
    Sheet1.Protect
    Application.EnableEvents = True
    End Sub
    
  • 现在,在同一窗口中的sheet1上单击鼠标右键,然后选择“插入”。从插入菜单中单击模块。现在选择创建模块并粘贴以下代码

    Public j
    
    Sub Settings()
    j = 0
    Sheet1.Unprotect
    Sheet1.Cells.Locked = True
    Sheet1.Range("D3").Locked = False
    Sheet1.Range("D3").Select
    Sheet1.Protect
    End Sub
    
  • 现在,每次要向工作表输入数据时,都要运行宏设置

  • 下面是代码的注释版本

    'The code below should come in a module and should be run every time a change is required in the sheet
    Public j 'declaring j as a public variable so that it can be accessed from any procedure in the excel project
    
    Sub Settings()
    j = 0 'sets j as zero
    Sheet1.Unprotect 'unprotect the sheet
    Sheet1.Cells.Locked = True 'locks all the cells in the sheet
    Sheet1.Range("D3").Locked = False 'unlocks the first cell D3 and makes it editable
    Sheet1.Range("D3").Select 'selects the cell D3
    Sheet1.Protect 'reprotects the sheet
    End Sub
    
    
    
    'Following code should be entered in the code of sheet where the values need to be entered
    Private Sub Worksheet_Change(ByVal Target As Excel.Range) 'the code runs when a cell in the excel sheet is changed
    On Error GoTo enditall 'this handles error
    Application.EnableEvents = False 'excel events are disabled
    i = Array("D3", "C3", "B9", "B3", "E2", "D4", "G4", "I4", "D5", "G5", "I5", "D6", "G6", "I6", "D7", "G7", "I7", "D8", "G8", "I8") 'array with cells in order
    k = Replace(Target.Cells.Address, "$", "") 'finds the cell address where the change was made
    If k = i(j) Then 'checks whether the change was made in a cell in the array, we set the value of j to zero by running the macro settings shown below
    Sheet1.Unprotect 'unprotects the sheet to make the changes
    Sheet1.Range(i(j)).Locked = True 'makes the correspondig cell in the array locked after editing
    Sheet1.Range(i(j + 1)).Locked = False 'unlocks the next cell in the array
    Sheet1.Range(i(j + 1)).Select 'selects the next cell in the array
    j = j + 1 'increments the value of j by 1
    Sheet1.Protect 'reprotects the sheet
    End If
    enditall: 'the code below will run on an error, this code will run when value of j becomes more than the number of elements in array k
    Sheet1.Protect 'protect the sheet
    Application.EnableEvents = True 'enables excel events
    End Sub
    

    查看位于

    的示例文件如果您想在不使用VBA的情况下检查订单,您可以在公式中使用数据验证(这将需要一些时间,但您将无需编写代码)

    • 选择要检查的第二个单元格(
      C3
    • 在功能区中,转至数据>数据验证
    • Allow:
      中,选择Custom
    • 在字段中,输入以下公式:
      =IF(ISEMPTY(D3),FALSE,TRUE)
    • 在“错误警报”选项卡中,更改对话框以向用户解释应执行的操作,例如:
    在填充单元格C3之前,必须先填充单元格D3

    有关数据验证的更多信息,请查看


    [编辑]最好的方法可能是创建vba,该vba将从数组自动创建这些验证

    如果您想在不使用vba的情况下检查订单,您可以将数据验证与公式一起使用(这将需要一段时间,但您将无需编写代码)

    • 选择要检查的第二个单元格(
      C3
    • 在功能区中,转至数据>数据验证
    • Allow:
      中,选择Custom
    • 在字段中,输入以下公式:
      =IF(ISEMPTY(D3),FALSE,TRUE)
    • 在“错误警报”选项卡中,更改对话框以向用户解释应执行的操作,例如:
    在填充单元格C3之前,必须先填充单元格D3

    有关数据验证的更多信息,请查看


    [编辑]最好的方法可能是创建vba,该vba将从阵列自动创建这些验证。计算机:对于您的最后一个问题,您可以使用Ctrl-H(搜索和Remplace)并选择选项
    查看公式
    ,然后替换所需内容。对我来说,解决此要求的明显方法是表单。创建一个看起来像给定屏幕截图的表单并不困难。有了表单,设计者就可以完全控制序列、后台处理和其他许多事情。有许多网站提供了创建表单的帮助,但如果你想要一个简单的例子,我可以提供答案。但是,如果你采取这种方法,你必须接受这样一个事实,那就是你必须花时间去学习宏。@brettdj:现在我也提供了顺序解决方案。@KannanS我不明白你在用代码做什么-你的j变量是空的,从不递增,所以你的
    如果k=I(j),那么
    只在D3上运行。您还需要满足两个或更多单元格的目标。(注:我将推翻我对工作代码的否决票)是的,你是对的。我已经提到,您必须运行“设置”宏来编辑工作表(参见第四点)。设置宏将j的值设置为0。这将从数组中提取第一个单元格。@FrankComputer:对于您的最后一个问题,您可以使用Ctrl-H(Search&Remplace)并选择选项
    查找公式
    ,然后替换所需内容。对我来说,这一要求的明显解决方案是表单。创建一个看起来像给定屏幕截图的表单并不困难。有了表单,设计者就可以完全控制序列、后台处理和其他许多事情。有许多网站提供了创建表单的帮助,但如果你想要一个简单的例子,我可以提供答案。但是,如果你采取这种方法,你必须接受这样一个事实,那就是你必须花时间去学习宏。@brettdj:现在我也提供了顺序解决方案。@KannanS我不明白你在用代码做什么-你的j变量是空的,从不递增,所以你的
    如果k=I(j),那么
    只在D3上运行。您还需要满足两个或更多ce的目标