相关数据包




使用VBA隐藏公式并保持特定单元格可编辑在本节中,我将演示如何使用VBA宏高效地隐藏工作表中公式栏中的公式,同时保持特定单元格的编辑能力。
步骤1:创建新模块
按“Alt” + “F11”打开“Visual Basic for Applications (VBA)”编辑器。 点击“插入” > “模块”以创建一个新模块。 步骤2:将VBA代码复制到模块窗口
复制以下VBA代码并粘贴到打开的“模块”窗口中。
Sub HideFormulasAndProtectWithEditableCells()
'Update by ExtendOffice
Dim xWs As Worksheet
Dim xWb As Workbook
Dim xPassword As String
xPassword = "123456" ' Replace "123456" with the actual password for protecting the sheet
Set xWb = Application.ActiveWorkbook
Set xWs = xWb.Sheets("Sheet1") ' Replace "Sheet1" with your sheet's name
xWs.Unprotect Password:=xPassword
For Each cell In xWs.UsedRange
If cell.HasFormula Then
cell.FormulaHidden = True
End If
cell.Locked = True
Next cell
On Error Resume Next
Set xEditableRange = Application.InputBox("Select the range to keep editable", "Kutools for Excel", Type:=8)
If Not xEditableRange Is Nothing Then
xEditableRange.Locked = False
End If
xWs.Protect Password:=xPassword, UserInterfaceOnly:=True
End Sub注意事项:
您应该将第6行的“123456”替换为实际用于保护工作表的密码。 您应该将第9行的“Sheet1”替换为您将要保护的实际工作表名称。 步骤3:运行VBA代码
在“模块”窗口中,按“F5”或点击 按钮以执行粘贴的代码。 在出现的“选择要保持可编辑的范围”对话框中,选择您希望保持可编辑的单元格,并点击“确定”。 结果
现在,使用您提供给VBA的密码,工作表已受到保护。此工作表中的所有公式均已隐藏,且所选单元格可编辑。