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

我希望用户只能按以下顺序将值input到下面列出的单元格中:

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

在这里输入图像说明

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

这里是使用vba控制顺序的代码

  1. 右键单击sheet1选项卡和“查看代码”。

  2. 将以下代码粘贴到该表单模块中。

    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 
  3. 现在在同一个窗口中右键单击sheet1并select插入。 从插入菜单上点击模块。 现在select创build模块并粘贴下面的代码。

     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 
  4. 现在运行macros设置,每次你想input数据到你的工作表。

这里是代码的评论版本

 '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 

请参阅https://docs.google.com/open?id=0B3mN8H2AV4UCN2E5ZWMxNjEtMGZiZS00NzYzLWI2NDUtOTdmZjg3YzcyNGUw上的示例文件

如果你想检查没有VBA的订单,你可以使用公式validation (这将需要一些时间,但你将没有代码来写 )。

  • select你想要检查的第二个单元格(在你的情况下是C3
  • 在function区中,转到数据>数据validation
  • Allow: ,select自定义
  • 在该领域,把这个公式: =IF(ISEMPTY(D3),FALSE,TRUE)
  • 在“ 错误警报 ”选项卡中,更改对话框以向用户解释他应该执行的操作,如下所示:

填充单元格C3之前,您必须填写单元格D3。

有关数据validation的一些额外信息,您可以在这里看看。

[编辑]最好的方法可能是创buildvba,将自动从数组中创build这些validation