【问题标题】:Cell.Formula is not recognised as a formula [duplicate]Cell.Formula 未被识别为公式[重复]
【发布时间】:2021-09-08 06:30:52
【问题描述】:

我正在使用以下 VBA 代码向单元格添加公式:

   Dim Rng As Range
   Dim cel As Range
   Dim i As Integer
   Dim AmountOfCells As Integer
   Dim varray As Variant
   Dim varrayValid As Variant
   Dim pctCompl1 As Single
   Dim pctCompl2 As Single
   Dim ref As Integer
   
   
   ' UPDATE COLUMN A
   
   Set Rng = Range("A12:A1521")
   ref = 9
    For Each cel In Rng.Cells
            With cel
                .Formula = "='Weekly Results'!E" & ref
                ref = ref + 1
            End With
            
    Next cel

“每周结果”是指有效的工作表。

但是,我的公式没有被评估为公式。当我手动选择一个单元格并从菜单栏中选择“评估公式”时,我收到一条消息,告诉我“正在评估的当前单元格包含一个常数”

我在代码Application.Calculation = xlAutomatic中设置了以下内容,在Options\Formulas\Workbook Calculation中设置为Automatic

如果我查找 '= 并将其替换为 =,则公式将正确计算。

谁能指出我正确的方向?

【问题讨论】:

  • 代码很好,对我有用。您是否正确声明了变量?将完整代码发布给您。
  • 我已经更新了帖子,添加了更多显示 defs 的代码。
  • 我已经在我的系统中尝试过您的代码,它运行良好。在应用公式之前尝试清除范围。如果您不涉及任何格式问题,请在 Set Rng = Range("A12:A1521") 之后使用 Rng.Clear
  • 从 A 列中删除“文本”格式。设置为“常规”。

标签: excel vba


【解决方案1】:

谢谢 mishu36,我添加了 'Rng.Clear',它似乎已经奏效了。

【讨论】:

  • 是的,因为它会删除您的“文本”格式。
猜你喜欢
  • 2021-10-19
  • 2022-01-13
  • 2019-08-21
  • 2023-02-11
  • 1970-01-01
  • 2020-12-22
  • 2010-11-01
  • 2021-06-30
  • 2016-01-31
相关资源
最近更新 更多