我希望只允许用户按以下顺序将值输入到下面列出的单元格中:
D3,C3,B9,B3,E2,D4,G4,I4,D5,G5,I5,D6,G6,I6,D7,G7,I7,D8,G8和I8。
答案 0 :(得分:5)
如果您想查看没有VBA 的订单,可以将数据验证与公式一起使用(这需要一些时间,但您将没有代码写强>)。
C3
)Allow:
中,选择自定义 =IF(ISEMPTY(D3),FALSE,TRUE)
在填充细胞C3之前必须填充细胞D3。
有关数据验证的一些额外信息,您可以查看here。
[编辑]最好的方法是创建vba,它将自动从数组中创建这些验证
答案 1 :(得分:2)
打开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并选择insert。从插入菜单中单击Module。现在选择创建模块并粘贴以下代码。
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
请参阅https://docs.google.com/open?id=0B3mN8H2AV4UCN2E5ZWMxNjEtMGZiZS00NzYzLWI2NDUtOTdmZjg3YzcyNGUw
上的示例文件