我需要有关 Vba 代码的帮助,以便在单元格G3
不包含单元格H3
,I3
和E3
上运行索引“字”。
如果单元格E3
包含一个“单词”,则应清除单元格G3
,H3
和I3
的公式并打开以供用户输入。
我希望保护工作表,因此不确定上述方法是否行得通。
另一个选项可能如下。
如果单元格E3
包含一个“单词”,则用户将收到一个消息框,以输入单元格G3
,H3
和I3
或其他{{1 }},G3
和H3
运行索引公式。
答案 0 :(得分:0)
下面的代码将检查E3的内容是否已更改,如果已更改,它将检查单元格是否包含文本“ Word”(不区分大小写),如果确实包含文本,则将清除单元格G3, H3和I3。
使用Worksheet_Change
事件将代码放在正在使用的工作表下:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$E$3" Then 'check if the value in cell E3 has changed
If InStr(UCase(Target), "WORD") > 0 Then
'check if the text "Word" (not case sensitive) is within the contents of the cell
Range("G3:I3").ClearContents 'if yes then clear G3:I3
MsgBox "Please fill the cells G3, H3 & I3", vbInformation, "Populate"
End If
End If
End Sub
更新:
根据您的评论,我现在更新了答案,下面的代码将锁定工作表中除E列之外的所有单元格,一旦E列中的单元格被填充,它将为该行解锁该列中的列。 G,H和I,因此用户可以输入必要的数据:
Sub LockCells()
'lock all cells apart from Column E
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
ws.Range("E:E").Locked = False 'leave unlocked
ws.Range("G:I").Locked = True 'lock these columns
ws.Protect Password:="xx", UserInterfaceOnly:=True
'change the password to whatever you wish
End Sub
Sub UnlockWorksheet()
'You might need this to unlock the sheet
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
ws.Unprotect Password:="xx"
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
If Target.Column = 5 Then 'check if the value in Column E has changed
If Target.Value <> "" Then
ws.Range("G" & Target.Row & ":I" & Target.Row).Locked = False
'check if the text "Word" (not case sensitive) is within the contents of the cell
ws.Range("G" & Target.Row & ":I" & Target.Row).ClearContents 'if yes then clear G:I
MsgBox "Please fill the columns G, H & I", vbInformation, "Populate"
End If
End If
End Sub