隐藏所选单元格中的公式

Hiding the formula in the selected cell

我的代码有错误,它说 .formula = TheFormula (variable) is not set。请帮我解决这个脚本。 我只想在单击单元格值时隐藏公式

     Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Dim Rng As Range
        Static Cell As Range
        Static TheFormula As String
        Set Rng = Range("n4:o25")
         If Not Application.Intersect(Target, Rng) Is Nothing Then
          If Not Cell Is Nothing Then
            Cell.Formula = TheFormula
          End If
         Set Cell = ActiveCell
        With Cell
         TheFormula = .Formula
          .Value = .Value
        End With
       Else
        With Cell
        .Formula = TheFormula
        End With
       End If
   End Sub

您在 if 之后:

    Set Cell = ActiveCell

但是您的 else 部分中缺少这一点。

使用以下代码。如果您 select 范围“n4:o25”中的任何单元格,它会删除公式(仅保留值),但如果您 select 其他内容,它什么都不做。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Rng As Range
Static Cell As Range
Set Rng = Range("n4:o25")
  If Not Application.Intersect(Target, Rng) Is Nothing Then
    Set Cell = ActiveCell
    If Not Cell Is Nothing Then
      Cell.Value = Cell.Value
    End If
  End If
End Sub