在Excel中隐藏公式(完整指南)

在Excel中隐藏公式(完整指南)

使用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的密码,工作表已受到保护。此工作表中的所有公式均已隐藏,且所选单元格可编辑。

相关内容

如何在Microsoft Word中绘图?详细方法分步教程
365bet官网开户网址

如何在Microsoft Word中绘图?详细方法分步教程

⌛ 07-31 👁️ 4436
《脱逃者2》游戏配置要求介绍
365bet官网开户网址

《脱逃者2》游戏配置要求介绍

⌛ 08-29 👁️ 1042